Trouble getting data outside of parfor loop

조회 수: 4 (최근 30일)
John Doe
John Doe 2013년 8월 27일
답변: John Doe 2013년 10월 28일
Hello, I have a process that needs to be applied to a 90 trials of an 11 electrode signal. The result of each trial is in the form of 11 different matrices of a number of rows that varies between trials (no greater than 12). I'm having trouble getting those matrices stored without matlab saying it can't be done whilst using parfor.
I create giant matrices of zeros outside the loop that has enough space for the max possible number of rows. Then inside the loop I try and pass the data to giantmatrix(rowNumber:rowNumber + row length of data, :).
clear all
matlabpool;
load MIdata25;
fs = 160;
numberElectrodes = 11;
Left1 = zeros(15*length(squeeze(thinkLeft(1, :, 1))), 1312);
Left2 = zeros(15*length(squeeze(thinkLeft(1, :, 1))), 1312);
Left3 = zeros(15*length(squeeze(thinkLeft(1, :, 1))), 1312);
Left4 = zeros(15*length(squeeze(thinkLeft(1, :, 1))), 1312);
Left5 = zeros(15*length(squeeze(thinkLeft(1, :, 1))), 1312);
Left6 = zeros(15*length(squeeze(thinkLeft(1, :, 1))), 1312);
Left7 = zeros(15*length(squeeze(thinkLeft(1, :, 1))), 1312);
Left8 = zeros(15*length(squeeze(thinkLeft(1, :, 1))), 1312);
Left9 = zeros(15*length(squeeze(thinkLeft(1, :, 1))), 1312);
Left10 = zeros(15*length(squeeze(thinkLeft(1, :, 1))), 1312);
Left11 = zeros(15*length(squeeze(thinkLeft(1, :, 1))), 1312);
parfor currentTrial = 1:length(squeeze(thinkLeft(1, :, 1)))
currentTrial
chanNum = 0;
signalL=[];
signalTotal=[];
for currentElectrode = [2 6 8 9 11 13 14 16 20 41 42]
%matrix indexing stuff
signalTotal = [signalTotal; signalL3'];
end
x = %process applied to signalTotal
for currentElectrode = 1:11
xxl = zeros(numberElectrodes, length(signalL(1, :)));
for imfNo = 1:length(squeeze(x(1, :, 1)))
%matrix re-indexing stuff
end
if currentElectrode == 1
Left1(((currentTrial-1)*15)+1:((currentTrial-1)*15)+length(xxl(:, 1)), :) = xxl;
elseif currentElectrode == 2
Left2(((currentTrial-1)*15)+1:((currentTrial-1)*15)+length(xxl(:, 1)), :) = xxl;
elseif currentElectrode == 3
Left3(((currentTrial-1)*15)+1:((currentTrial-1)*15)+length(xxl(:, 1)), :) = xxl;
elseif currentElectrode == 4
Left4(((currentTrial-1)*15)+1:((currentTrial-1)*15)+length(xxl(:, 1)), :) = xxl;
elseif currentElectrode == 5
Left5(((currentTrial-1)*15)+1:((currentTrial-1)*15)+length(xxl(:, 1)), :) = xxl;
elseif currentElectrode == 6
Left6(((currentTrial-1)*15)+1:((currentTrial-1)*15)+length(xxl(:, 1)), :) = xxl;
elseif currentElectrode == 7
Left7(((currentTrial-1)*15)+1:((currentTrial-1)*15)+length(xxl(:, 1)), :) = xxl;
elseif currentElectrode == 8
Left8(((currentTrial-1)*15)+1:((currentTrial-1)*15)+length(xxl(:, 1)), :) = xxl;
elseif currentElectrode == 9
Left9(((currentTrial-1)*15)+1:((currentTrial-1)*15)+length(xxl(:, 1)), :) = xxl;
elseif currentElectrode == 10
Left10(((currentTrial-1)*15)+1:((currentTrial-1)*15)+length(xxl(:, 1)), :) = xxl;
elseif currentElectrode == 11
Left11(((currentTrial-1)*15)+1:((currentTrial-1)*15)+length(xxl(:, 1)), :) = xxl;
end
end
midpointL(currentTrial, :) = length(xxl(:, 1));
end
With matlab showing problems with Left1-9, though it seems fine with midpointL.
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 8월 27일
Try replacing the length(xx1(:,1)) with numberElectrodes since you built xx1 with numberElectrodes as the first dimension.

댓글을 달려면 로그인하십시오.

채택된 답변

John Doe
John Doe 2013년 10월 28일
The answer to this was that matlab does not like "parfor variable +/- integer" as it thinks it'll be used to reference a previous loop which it can't do in parallel. In this case it's the variable CurrentTrial. Creating a 3d matrix instead of a 2d one with CurrentTrial used to define the first column solved it.

추가 답변 (1개)

Matt J
Matt J 2013년 8월 27일
편집: Matt J 2013년 8월 28일
I create giant matrices of zeros outside the loop that has enough space for the max possible number of rows. Then inside the loop I try and pass the data to giantmatrix.
A better idea would probably be to dispense with Left1...9 and instead store the results (of different sizes) in a 90x11 cell array. Then combine them after the loops end as needed.
N=length(squeeze(thinkLeft(1, :, 1)));
LoopData=cell(N, 11);
parfor currentTrial = 1:N
for currentElectrode = 1:11
xxl = ...;
LoopData{currentTrial,currentElectrode}=xxl;
end
end
Now you can combine all the different xxl in LoopData using cell2mat() or similar...

카테고리

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