How to loop on vector values and have indices too

Click For Summary
SUMMARY

The discussion focuses on looping through a vector in MATLAB while also retrieving the index of each element. The user provides a vector K = [3 5 7 9 11] and seeks to display both the value and its corresponding index during iteration. The solution involves using a for loop that can access the index directly, allowing the user to know the current index while iterating through the vector.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with vector data structures in MATLAB
  • Knowledge of for loop syntax in MATLAB
  • Understanding of indexing in MATLAB arrays
NEXT STEPS
  • Learn how to use MATLAB's "for" loop with index access
  • Explore MATLAB's array indexing techniques
  • Investigate MATLAB's built-in functions for vector manipulation
  • Study examples of nested loops in MATLAB for advanced indexing
USEFUL FOR

This discussion is beneficial for MATLAB programmers, data analysts, and anyone interested in efficient vector manipulation and indexing in MATLAB.

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 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 29 ·
Replies
29
Views
5K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 36 ·
2
Replies
36
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K