Help with FOR to PARFOR conversion

2 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by