如何在Matlab parfor循环中对矩阵进行分类? [英] How to classify a matrix within a Matlab parfor loop?
问题描述
我正在寻找对矩阵值进行分类的方法.以下示例在parfor
循环之外运行,但是在parfor
循环中使用时不起作用.在提供的示例之后,在parfor
循环中,对矩阵进行分类有什么选择?
I am looking to classify the values of a matrix. The following example works outside of a parfor
loop, however it does not work when used within a parfor
loop. What are my options to classify matrices, following the provided example, within a parfor
loop?
% Sample data
Imag1 = [ 62 41 169 118 210;
133 158 96 149 110;
211 200 84 194 29;
209 16 15 146 28;
95 144 13 249 170];
% Perform the classification
Imag1(find(Imag1 <= 130)) = 4;
Imag1(find(Imag1 >= 150)) = 1;
Imag1(find(Imag1 > 140)) = 2;
Imag1(find(Imag1 > 130)) = 3;
结果如下(在parfor
循环之外):
Results in the following (outside parfor
loop):
Imag1 =
62 41 169 118 210
133 158 96 149 110
211 200 84 194 29
209 16 15 146 28
95 144 13 249 170
Imag1 =
4 4 1 4 1
3 1 4 2 4
1 1 4 1 4
1 4 4 2 4
4 2 4 1 1
推荐答案
您可以使用另一个矩阵来存储结果.
You can use another matrix to store the result.
Imag2 = zeros(size(Imag1));
% Perform the classification
Imag2(find(Imag1 <= 130)) = 4;
Imag2(find(Imag1 >= 150)) = 1;
Imag2(find(Imag1 > 140)) = 2;
Imag2(find(Imag1 > 130)) = 3;
,因此您可以以某种方式使用parfor循环.由于parfor不在乎执行顺序.您的代码无法在parfor循环中工作,因为现在您正在修改Imag1的值,然后再次比较这些值,也就是说,任何循环迭代都将取决于先前的循环迭代
so you can use a parfor loop somehow. Since parfor doesn't care about order of execution. Your code doesn't work in a parfor loop because right now you are modifying the values of Imag1, then comparing those values again, aka any loop iteration would be be dependent on the prior loop iterations
这篇关于如何在Matlab parfor循环中对矩阵进行分类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!