How to loop on vector values and have indices too

AI Thread Summary
To loop through the vector K = [3 5 7 9 11] while also accessing the index of each value, a common approach in MATLAB is to use a for loop with the length of the vector. By iterating over the indices, you can display both the current value and its corresponding index. The suggested method involves using a loop that accesses each element by its index, allowing you to print the value alongside its index. This way, when K equals 9, you can identify it as index 4. The discussion emphasizes the need for clarity in accessing both the value and its index during the loop execution.
funyhony
Messages
7
Reaction score
0
i want to loop on K vector values
K = [3 5 7 9 11]

and have with me inside the loop the index of the current vector value

if i made
for K = [3 5 7 9 11]
disp(K)
end

i'll not have the index of K values

i mean when K=9 i want to know that I'm in index 4

how to make that??
 
Physics news on Phys.org
What language are you looking to do this in? If this is MATLAB, your output would actually display the index value (K) with every iteration of the loop. Given how quickly this would execute, you'll probably just see 3, 5, 7, 9, 11 printed on successive lines.
 

Similar threads

Replies
2
Views
1K
Replies
10
Views
3K
Replies
10
Views
4K
Replies
36
Views
254
Replies
10
Views
3K
Replies
6
Views
4K
Back
Top