1

In Java I can directly loop over elements in an array, e.g.:

int[] array = [1,2,3,4];
for (int elem: array){
  System.out.println(elem);
}

Instead of looping via indices

int[] array = [1,2,3,4];
for(int i=0; i<array.length; i++){
  System.out.println(array[i]);
}

How can I do the same in MATLAB?

3
  • 1
    In my eyes the down vote is justified. Instead of asking "How can I do the same in MATLAB?" you could just have tried to actually DO it, the documentation is very clear on everything you asked. This question does not show any effort and just because you got a very good answer (though it just extends the doc) - doesn't make the question better. Commented Oct 3, 2014 at 12:14
  • Agreed, documentation is clear. I did not come across it. And as far as I understand the SO down votes have to be used with caution and extreme cases -- as I understand as a SO user I should help improve the questions rather than throwing them out throw them by down voting. I am pretty sure that expert SO users will agree with me. Question like "what did you try" in the comments can help improve questions and as well as teach new users to write better questions. Commented Oct 3, 2014 at 12:40
  • 1
    @Bob, downvotes on questions should not be used with extreme caution. Have a look at this meta question (and answer). Meta is mostly visited by what you would refer to as "expert SO users", so I would say it's safe to say that expert users won't necessarily agree with you. When that's said, leaving a comment when downvoting explaining the reason is a nice gesture. Also take a look at this meta post explaining how the community tries to Optimize for pearls, not sand. Commented Oct 3, 2014 at 13:10

1 Answer 1

5

In Matlab the two options analogous to those in your question are:

%// Loop via indices
array = [10 20 30 40];
for n = 1:numel(array)
   disp(array(n));
end

%// Loop over elements directly    
array = [10 20 30 40];
for elem = array
   disp(elem);
end

Some notes:

  1. When looping via indices, the statement for n = 1:1000 does not create the whole array 1:1000. So you can safely write for example for n = 1:inf (and maybe then use a break within the loop).

  2. When an array is proveded in the loop statement, Matlab loops over columns. So for example

    %// Loop over elements (columns) directly
    array = [10 20 30 40; 50 60 70 80];
    for elem = array
       disp(elem);
    end
    

    would set elem to [10; 50], then [20; 60] etc

  3. The array can be any type, not necessarily numeric (and the column rule still applies). For example,

    %// Loop over elements (columns) directly, using a cell array
    array = {'Hello', 'Hi'; 'Goodbye' 'Godspeed'};
    for elem = array
       disp(elem);
    end
    

    makes elem equal to the column cell array {'Hello'; 'Goodbye'} and then to {'Hi'; 'Godspeed'}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.