Help with FOR to PARFOR conversion

조회 수: 1 (최근 30일)
dsmalenb
dsmalenb 2020년 8월 7일
댓글: Walter Roberson 2020년 8월 8일
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