1

In python one can use zip to loop multiple vectors or enumerate to get the current index of the looped vector like so

one = ['A', 'B', 'C']
two = [1, 2, 3]

for i, j in zip(one, two):
    print i, j

for j, i in enumerate(one):
    print i, two[j]

Gives

>>> 
A 1
B 2
C 3
A 1
B 2
C 3

In MATLAB it's possible to do

one = {'A' 'B' 'C'};
two = [1 2 3];

for i = 1:1:length(one)
  printf('%s %i\n', one{i}, two(i));
endfor

j = 1;
for i = one
  printf('%s %i\n', i{1}, two(j));
  j = j + 1;
endfor

giving

A 1
B 2
C 3
A 1
B 2
C 3

So is one of those two options the common way how one would do it in MATLAB, i. e. to loop through several vectors "in parallel" or is there another, maybe better way?

Bonus:

two = [1 2 3];
two = [1, 2, 3];

Both of these lines give the same output in the upper MATLAB program. Whats the difference?

embert
  • 7,336
  • 10
  • 49
  • 78
  • Do you want two "spaces" between `A` and `1`, `B` and `2` and so on, or something else as the separator between them? BTW I don't think MATLAB has printf or does it? – Divakar Aug 10 '14 at 04:26
  • @Divakar Okay, it's octave code (cause matlab is too exp to have it at home), but basically I use matlab at work. Anyway, it's not about the print function, but about the best way to loop. Seems to me the first option is a good one and then simply use `i` as index to access the elements of the vectors, lists or whatever.. – embert Aug 10 '14 at 04:36
  • Ok that's fine. So what about the separator? – Divakar Aug 10 '14 at 04:38
  • @divakar not sure I got you right. The separator (when printing) is given by the string in `printf`. What I wanted to know is if using `,` or whitespace as separator inbetween `[]` both give vectors? Maybe it's an octave specific thing. – embert Aug 10 '14 at 04:43
  • Well you are using `printf('%s %i\n'`..)`, so it seem there is "one space" between printing `one` and `two` elements. If you were using `printf('%s:%i\n'`, you are using `:` as the separator. So, my question was - What is the separator that you want for printing? – Divakar Aug 10 '14 at 04:46
  • If it's a general question on how to loop through vectors with some "parallel" way, it depends on what you have inside the loop. But if you would like to stick to for-loop, I would say go ahead with the first one of those two methods. – Divakar Aug 10 '14 at 05:03
  • if you have vectors of similiar type then you can generate a n-dimension grid using `ngrid` (see the documentation here --http://www.mathworks.in/help/matlab/ref/ndgrid.html) for n vectors and then refer directly to them using the set of corresponding indices from each vector, as matlab is more efficient in vector calculation than loops. – Nishant Aug 10 '14 at 05:59
  • @embert - `two = [1 2 3];` and `two = [1,2,3];` are equivalent in MATLAB / Octave. It's just a matter of style and preference. In Python, you have to explicitly place commas (of course, you already knew that). @Yvon showed in his post that you can mix placing commas and spaces when creating matrices / arrays and you still get the same result. – rayryeng Aug 10 '14 at 07:23

1 Answers1

1

Using printf, or fprintf in Matlab, is pretty good. The Matlab code for your first approach is

one = {'A' 'B' 'C'};
two = [1 2 3];

for ii = 1:length(one)
  fprintf('%s %i\n', one{ii}, two(ii));
end

It's also possible to put the strings into a cell array, without any for loop.

s = cellfun(@(a,b) [a,' ',b], one', ...
    arrayfun(@num2str, two', 'UniformOutput', false),....
    'UniformOutput', false)

Bonus:

>> A = [1;2;3]   
A =
     1
     2
     3
>> A = [1 2 3]   
A =
     1     2     3
>> A = [1,2,3]   
A =
     1     2     3
>> A = [1,2,3;4 5 6;7,8 9]
A =
     1     2     3
     4     5     6
     7     8     9
>> 

Bonus 2:

Using i and j is bad. See - Using i and j as variables in Matlab

Community
  • 1
  • 1
Yvon
  • 2,903
  • 1
  • 14
  • 36