0

I would like to do something with each element in an array. For a row array, I can do this:

array = [1 2 3];
i = 0;
for a = array
    i = i + 1;
end
fprintf('Number of iterations: %g\n', i)

Number of iterations: 3

It will output 3, so it actually accessed each array element one after another.

However if the array is a column, the same code will output just 1:

array = [1; 2; 3];
...

Number of iterations: 1

I wonder why exactly this happens and if there is a way to iterate through an array, independent from its "directional dimension" and without using for i = 1:numel(array), a = array(i).

texnic
  • 3,959
  • 4
  • 42
  • 75
  • 2
    if you're dealing only with row- or column-vectors you could use `for a = array(:).'` to make sure you work on a row vector, since Matlab iterates over columns. – Benoit_11 Jan 25 '16 at 18:29
  • Consult chappjc's answer in the duplicate I have marked... specifically towards the end. There are some intricacies you need to keep in mind when looping if you are using matrices as access indices into them. @Benoit_11 however did mention a `tl;dr` comment summarizing the duplicate. – rayryeng Jan 25 '16 at 18:53
  • ahh @rayryeng I knew there was a duplicate somewhere! thanks haha – Benoit_11 Jan 25 '16 at 19:00
  • @Benoit_11 :D only because I knew what to look for. I read that question/answer in the past and it's in my favourites. – rayryeng Jan 25 '16 at 19:02
  • @Benoit_11, why do you have a dot in your comment after `array(:)`? Is there any difference if I use simply `array(:)'`? – texnic Jan 25 '16 at 21:36
  • @texnic that's a habit of mine, using the [nonconjugate transpose](http://www.mathworks.com/help/matlab/ref/ctranspose.html) instead of the conjugate transpose. When working with imaginary numbers it can be handy but when using real-only numbers there is no difference. – Benoit_11 Jan 25 '16 at 21:47
  • 2
    @texnic When dealing with real numbers, `'` and `.'` make no difference. However if you have complex valued arrays, doing `.'` is what is known as the non-conjugate transpose. Doing `'` on complex valued arrays not only transposes but also **negates** the imaginary component. Doing `.'` maintains the sign of the imaginary component when transposing. This small oversight has bit me in the @$$ in the past because I was unaware of this behaviour so I now just use `.'` due to force of habit. – rayryeng Jan 25 '16 at 21:49
  • @rayryeng, BTW, I wonder if marking this question a duplicate is correct. It sure relates to the other one, but the question itself is completely different. There is a very little chance that a person would find the other question looking for what I asked. Besides, even having found that question, you need to read it in detail to solve my problem. – texnic Jan 26 '16 at 13:14

1 Answers1

1

When a for loop is initialized with an array, it iterates column by column. This may not be what you want, but that the built in behavior (see http://www.mathworks.com/help/matlab/ref/for.html).

You can force your matrix into a linear row vector, so MATLAB will iterate the elements 1 by 1 with the following:

for i = A(:)'
    i % use each value of A
end

Normally, a combination of vector operations will be faster than a for loop, so only use a for loop when you can't think of the appropriate vector operation equivalent.

gslavin
  • 701
  • 6
  • 4