Help with FOR to PARFOR conversion

2 次查看(过去 30 天)
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!

回答(1 个)

Walter Roberson
Walter Roberson 2020-8-8
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 个评论
Walter Roberson
Walter Roberson 2020-8-8
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by