Can parallel traversals be done in MATLAB just as in Python?

Using the zip function, Python allows for loops to traverse multiple sequences in parallel.

for (x,y) in zip(List1, List2):

Does MATLAB have an equivalent syntax? If not, what is the best way to iterate over two parallel arrays at the same time using MATLAB?


Asked by: Ryan906 | Posted: 27-01-2022






Answer 1

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

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

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

Here is an 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
>> 

Answered by: Kellan271 | Posted: 28-02-2022



Answer 2

Tested only in octave... (no matlab license). Variations 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

Answered by: Emma433 | Posted: 28-02-2022



Answer 3

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

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

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

or whatever you have to do with the data.

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

Answered by: First Name849 | Posted: 28-02-2022



Answer 4

I would recommend to join the two arrays 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 if your functions can work with vectors. Then again, many functions can even work with matrices, so you wouldn't even need the loop.

Answered by: Leonardo150 | Posted: 28-02-2022



Answer 5

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 the cell is more than 2x2, and the cell is even transposed. Please try this.

And 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

Answered by: Lenny184 | Posted: 28-02-2022



Answer 6

If I have two arrays al and bl with same dimension No 2 size and I want to iterate through this dimension (say multiply al(i)*bl(:,i)). Then 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

Answered by: Lana622 | Posted: 28-02-2022



Answer 7

for loops in MATLAB used to be slow, but this is not true anymore.

So vectorizing is not always the miracle solution. Just use the profiler, and tic and toc functions to help you identify possible bottlenecks.

Answered by: Emma387 | Posted: 28-02-2022



Similar questions

python - Recursion - how do you extract from specific traversals

Lets say you have a string: abcde And a set of strings: ab, cde, abcd, a, bcd, cd You want to find all possible concatenations from the set that form the string. You can use recursion to traverse through all possible concatenations from the set, but how would you return only those that satisfy the solution? The possible combinations: ab - cde - yes ab - cd - no abcd - no a ...


python - Writing traversals to a file in python3.x issue

I am trying to traverse a heap, and write the traversal to a file but I am failing miserably. I keep getting an issue with the maximum traversal depth that spams my terminal when all I want is for the node to be printed out in the file.


recursion - Traverse tree and return a node instance after n traversals in python

The end goal is to copy a node from one tree to another tree. I want to visit each node in a binary tree and return a node instance after a number of traverses. I cannot seem to figure out how to return a specific node. Every time the node returned matches the id of the root node since I pass the root node to the function. class node(): def __init__(self): self.parent = None self.left =...


algorithm - Tree Traversals, recursion is faster than iteration in python?

I have implement tree preorder traversal in python, but found that my recursive version is faster than iteration version. code is as below: from __future__ import print_function import time class Tree(): def __init__(self, value): self.value = value self.left = None self.right = None def build_tree(string): nodes = [0] + [Tree(s) for s in string] for i in range...


python - Can a directed graph have two DFS traversals?

Here is my DFS traversal algorithm (Recursive approach): def dfs(v,visited) : for i in Graph[v] : if i not in visited : visited.append(i) print(i) dfs(i,visited) n = int(input()) Graph = {} for i in range(n) : name= input(print("Enter ",i+1," vertex name")) list...


python - Traversals of Binary Trees

I am trying to implement in-order, pre-order, and post-order traversals of a binary tree. My implementation seems to work for trees with a small number of nodes (as I have checked by hand) but the online software I am using says there are cases that my implementation fails (it does not provide the input that causes my implementation to fail). I have looked through several other threads on this site but cannot seem to find ...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top