Help with FOR to PARFOR conversion

2 views (last 30 days)
dsmalenb
dsmalenb on 7 Aug 2020
Commented: Walter Roberson on 8 Aug 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!

Answers (1)

Walter Roberson
Walter Roberson on 8 Aug 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 Comment
Walter Roberson
Walter Roberson on 8 Aug 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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!