# problem in using parralel computing toolbox

1 次查看（过去 30 天）
Abhinav 2017-11-6

I want to speed up my computations using parfor. I am using the code written below,
l=31:1:1000; % length-scale
sig2=6.5:0.1:10; % noise-variance
sigf2=1.3:0.1:10; % signal-variance
L=zeros(length(l),length(sig2),length(sigf2));
par=zeros(length(l),length(sig2),length(sigf2));
lsig2=length(sig2);
lsigf2=length(sigf2);
% cross-validation
parfor i=1:length(l)
for j=1:lsig2
for k=1:lsigf2
par(i,j,k)=[l(i),sig2(j),sigf2(k)];
L(i,j,k)=kcrossval(Xtrain,ytrain,M,l(i),sig2(j),sigf2(k));
end
end
end
but it is not working. I get error:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts.
When I remove the code
par(i,j,k)=[l(i),sig2(j),sigf2(k)];
it works but gives the following warning:
the entire array or struct 'sig2' is a broadcast variable. This might result in unnecessary communication overhead.
I have read the documentation but not able to find a solution. Please suggest the solutions.

### 采纳的回答

OCDER 2017-11-6

You are assigning 3 values, l(i),sig2(j),sigf2(k), into one value at par(i, j, k), which is not possible.
par(i,j,k)=[l(i),sig2(j),sigf2(k)];
But you don't really need this, since you know what values were used. If you want this, change par into a cell array instead, and do:
par{i,j,k} = ...;
The broadcast variable warning is simply warning you that a constant vector is being passed to every worker every time, hence could be slow (especially if they're large matrices). In your case, sig2 and sigf2 are pretty small, so don't worry about this too much. This warning appears when you are accessing a small segment of a vector per loop. Notice that Xtrain and ytrain are not give you warning, because parfor knows these are broadcast variables (you aren't accessing " Xtrain(j)", but rather the full Xtrain vector).
To suppress these warnings, you could explicitly tell Matlab these are broadcast variables via another temporary variable like this:
parfor i=1:length(l)
sig2Temp = sig2;
sigf2Temp = sigf2;
for j=1:lsig2
for k=1:lsigf2
L(i,j,k)=kcrossval(Xtrain,ytrain,M,l(i),sig2Temp(j),sigf2Temp(k));
end
end
end
OCDER 2017-11-6
You're welcome!

### 类别

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!