# [ACCEPTED]-Can parallel traversals be done in MATLAB just as in Python?-for-loop

Score: 20

If x and y are column vectors, you can do:

``````for i=[x';y']
# do stuff with i(1) and i(2)
end
``````

(with 2 row vectors, just use `x` and `y`).

Here is an 1 example run:

``````>> x=[1 ; 2; 3;]

x =

1
2
3

>> y=[10 ; 20; 30;]

y =

10
20
30

>> for i=[x';y']
disp(['size of i = ' num2str(size(i)) ', i(1) = ' num2str(i(1)) ', i(2) = ' num2str(i(2))])
end
size of i = 2  1, i(1) = 1, i(2) = 10
size of i = 2  1, i(1) = 2, i(2) = 20
size of i = 2  1, i(1) = 3, i(2) = 30
>>
``````
Score: 8

Tested only in octave... (no matlab license). Variations 1 of arrayfun() exist, check the documentation.

``````dostuff = @(my_ten, my_one) my_ten + my_one;

tens = [ 10 20 30 ];
ones = [ 1 2 3];

x = arrayfun(dostuff, tens, ones);

x
``````

Yields...

``````x =

11   22   33
``````
Score: 6

If I'm not mistaken the zip function you 10 use in python creates a pair of the items found in list1 and list2. Basically it still is a 9 for loop with the addition that it will 8 retrieve the data from the two seperate 7 lists for you, instead that you have to 6 do it yourself.

So maybe your best option 5 is to use a standard for loop like this:

``````for i=1:length(a)
c(i) = a(i) + b(i);
end
``````

or whatever 4 you have to do with the data.

If you really 3 are talking about parallel computing then 2 you should take a look at the Parallel Computing Toolbox for matlab, and 1 more specifically at parfor

Score: 2

I would recommend to join the two arrays 5 for the computation:

``````% assuming you have column vectors a and b
x = [a b];

for i = 1:length(a)
% do stuff with one row...
x(i,:);
end
``````

This will work great 4 if your functions can work with vectors. Then 3 again, many functions can even work with 2 matrices, so you wouldn't even need the 1 loop.

Score: 0
``````for (x,y) in zip(List1, List2):
``````

should be for example:

``````>> for row = {'string' 10
>>           'property' 100 }'
>>    fprintf([row{1,:} '%d\n'], row{2, :});
>> end
string10
property100
``````

This is tricky because 3 the cell is more than 2x2, and the cell 2 is even transposed. Please try this.

And 1 this is another example:

``````>> cStr = cell(1,10);cStr(:)={'string'};
>> cNum=cell(1,10);for cnt=1:10, cNum(cnt)={cnt};
>> for row = {cStr{:}; cNum{:}}
>>    fprintf([row{1,:} '%d\n'], row{2,:});
>> end
string1
string2
string3
string4
string5
string6
string7
string8
string9
string10
``````
Score: 0

If I have two arrays al and bl with same 3 dimension No 2 size and I want to iterate 2 through this dimension (say multiply `al(i)*bl(:,i)`). Then 1 the following code will do:

``````al = 1:9;
bl = [11:19; 21:29];

for data = [num2cell(al); num2cell(bl,1)]

[a, b] = data{:};
disp(a*b)

end
``````

More Related questions