在 MATLAB 中可以像在 Python 中一样进行并行遍历吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can parallel traversals be done in MATLAB just as in Python?
提问by Jason
Using the zipfunction, Python allows for loops to traverse multiple sequences in parallel.
使用该zip函数,Python 允许循环并行遍历多个序列。
for (x,y) in zip(List1, List2):
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?
MATLAB 有等效的语法吗?如果没有,使用 MATLAB 同时迭代两个并行数组的最佳方法是什么?
回答by mattiast
If x and y are column vectors, you can do:
如果 x 和 y 是列向量,您可以执行以下操作:
for i=[x';y']
# do stuff with i(1) and i(2)
end
(with row vectors, just use xand y).
(对于行向量,只需使用xand 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
>>
回答by DMC
Tested only in octave... (no matlab license). Variations of arrayfun() exist, check the documentation.
仅在八度音阶中测试...(无 matlab 许可证)。存在 arrayfun() 的变体,请查看文档。
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
回答by sven
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.
如果我没记错的话,您在 python 中使用的 zip 函数会创建一对在 list1 和 list2 中找到的项目。基本上它仍然是一个 for 循环,另外它会为你从两个单独的列表中检索数据,而不是你必须自己做。
So maybe your best option is to use a standardfor loop like this:
所以也许你最好的选择是使用这样的标准for 循环:
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 Toolboxfor matlab, and more specifically at parfor
如果你真的在谈论并行计算,那么你应该看看Parallel Computing Toolboxfor matlab,更具体地说是parfor
回答by bastibe
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.
如果您的函数可以使用向量,这将非常有用。再说一次,许多函数甚至可以处理矩阵,因此您甚至不需要循环。
回答by Sean
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.
这很棘手,因为单元格大于 2x2,并且单元格甚至转置了。请试试这个。
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
回答by Sean
forloops in MATLAB used to be slow, but this is not true anymore.
forMATLAB 中的循环曾经很慢,但现在不再如此。
So vectorizing is not always the miracle solution. Just use the profiler, and ticand tocfunctions to help you identify possible bottlenecks.
所以矢量化并不总是奇迹般的解决方案。只需使用分析器tic和toc函数来帮助您识别可能的瓶颈。

