Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to loop on vector values and have indices too

  1. Jun 27, 2011 #1
    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]

    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??
  2. jcsd
  3. Jun 29, 2011 #2


    User Avatar
    Science Advisor

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook