Help with FOR to PARFOR conversion

1 visualización (últimos 30 días)
dsmalenb
dsmalenb el 7 de Ag. de 2020
Comentada: Walter Roberson el 8 de Ag. de 2020
Hi there!
I have looked at several examples on how to convert a for loop into a parfor but for some reason I cannot get mine working. I have supplied an example of what I have working in my for loop below and my attenpt at the parfor loop. It has been a little while since I have used parfors so if anyone can assist, I would be very thankful.
for j = 1:N
j
clear D;
[signal fs] = audioread(strcat(S(j).folder, '/', S(j).name));
signal = signal(:,1);
D(:,1) = spectralCentroid(signal,fs);
D(:,2) = abs(spectralSlope(signal,fs));
D = D./max(D);
D(any(isnan(D), 2), :) = [];
end
parfor j = 1:N
j
[signal fs] = audioread(strcat(S(j).folder, '/', S(j).name));
signal = signal(:,1);
D{j}(:,1) = spectralCentroid(signal,fs);
D{j}(:,2) = abs(spectralSlope(signal,fs));
% Eliminate NaN rows
D{j}(any(isnan(D{j}),2),:);
% Normalize descriptors for hypercube
% D = D./max(D);
end
The latter does not run If I comment out
D{j}(any(isnan(D{j}),2),:);
until I have fun the parfor and then run the follwoing in the immediate window
D{1}(any(isnan(D{1}),2),:) = [];
then it works. I am not sure why.
Thank you for your help!

Respuestas (1)

Walter Roberson
Walter Roberson el 8 de Ag. de 2020
Every iteration you overwrite all of D. Your result is whatever was assigned in the last iteration. But the order of iterations is not not fixed for parfor, so the results would be random if the loop was permitted at all.
  1 comentario
Walter Roberson
Walter Roberson el 8 de Ag. de 2020
You are currently building values directly into parts of D{j}. You should instead build into a temporary variable, and assign the variable to D{j} once completely built.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by