How to change this for loop into parfor loop?

1 Ansicht (letzte 30 Tage)
Will
Will am 6 Dez. 2017
Beantwortet: Will am 9 Dez. 2017
Hi guys, I'm new to the MATLAB. I am currently trying to change a for loop into a parfor loop, but after many attempts altering my codes, I still cannot seem to make it work. I either get variable classification issue or I get transparency issue. I hope that someone can help me translate my codes into a parfor loop. Here are the codes:
% Removing colums of data with y consecutive data points that have same values with in each column. data.txt consist of a n by m matrix with the first row being names.
load data.txt
nrow=size(data,1);
ncol=size(data,2);
y=10;
for j = 1:1:ncol
i = 2;
while i <= nrow - (y - 1)
if data(i,j) == data(i+1,j)
data_vec = data(i:i+y-1,j);
diff_vec = data_vec(2:end,1) - data_vec(1:end-1,1);
temp = sort(diff_vec);
if temp(1,1) ==0 && temp(end,1) == 0
data(:,j) = 0;
i = nrow;
else
i = i + find(diff_vec,1) - 1;
end
clearvars temp data_vec diff_vec
end
i = i + 1;
end
end
data( :, ~any(data,1) ) = [];
clearvars nrow ncol j i
Thank you for your help in advance :).

Akzeptierte Antwort

Looky
Looky am 6 Dez. 2017
Bearbeitet: Looky am 6 Dez. 2017
There are two issues in your code that prevent the parfor loop from working.
  1. The matrix data is indexed in a way that matlab considers to complicated to be sure what part of the matrix must be sent to each worker. You can circumvent this in a rather brutal way by using a temporary variable that stores the column of the matrix that is currently processed and work with this instead (called dummy, see below)For further reading: Sliced Variables
  2. Matlab does not allow clear within the parfor loop. This causes the transparency errors. In your case you can easily remove the clear within the parfor loop, it has no use other than some additional unnecessary memory allocating.
Accounting for both issues, the code should look something like this:
nrow=size(data,1);
ncol=size(data,2);
y=10;
parfor j = 1:1:ncol
i = 2;
dummy=data(:,j);
while i <= nrow - (y - 1)
if dummy(i) == dummy(i+1)
data_vec = dummy(i:i+y-1);
diff_vec = data_vec(2:end,1) - data_vec(1:end-1,1);
temp = sort(diff_vec);
if temp(1,1) ==0 && temp(end,1) == 0
data(:,j) = 0;
i = nrow;
else
i = i + find(diff_vec,1) - 1;
end
end
i = i + 1;
end
end
data( :, ~any(data,1) ) = [];
clearvars nrow ncol j i
Please comment if this solves your problem or not.

Weitere Antworten (1)

Will
Will am 9 Dez. 2017
Hi Looky,!
Thank you for you reply.
I actually did a similar thing with the dummy variable but didn't know the clearvars was illegal in a parfor loop (must have missed the part explaining that!). My codes were able to run without error after removing the clearvars. Run time went from around 30 seconds to 0.6 seconds. I also doubled checked the results and indeed the results are identical to the original code.
Again, thanks for the help! Really appreciated it.

Kategorien

Mehr zu Debugging and Analysis finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by