- 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
- 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.
How to change this for loop into parfor loop?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 :).
0 comentarios
Respuesta aceptada
Looky
el 6 de Dic. de 2017
Editada: Looky
el 6 de Dic. de 2017
There are two issues in your code that prevent the parfor loop from working.
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.
0 comentarios
Más respuestas (1)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!