Matlab parfor嵌套循环变量访问
Matlab parfor nested loop variable access
我需要在 MATLAB 中使用 parfor
为外部循环编写一个简单的嵌套 for 循环。骨架代码为:
parfor i=1:M
for j=1:N
A(i,j)=myFunction(i,j);
end
end
在此之后,我需要找到矩阵A
中的最大值(对应的行号和列号)。但是,这个变量在 parfor
循环之外是不可访问的。最简单的方法是什么?这是分类器的多参数调整所必需的。
更新
这里是确切的代码:
C1=[0.001;100]; C2=[0.001;100];
A=zeros(length(C1),length(C2));
parfor i=1:length(C1)
for j=1:length(C2)
A(i,j)=2*i-3*j;
end
end
[max_num,max_idx] = max(A(:)); %finding maximum element
[X Y]=ind2sub(size(A),max_num); %retrieving location of max element
% Need to use these values
bestC=C1(X)
bestD=C2(Y)
poolobj = gcp('nocreate');
delete(poolobj);
这给出了错误:
Error: The variable A in a parfor cannot be classified.
稍作修改,Matlab 能够理解您的代码。
C1=[0.001;100]; C2=[0.001;100];
n=length(C1);
m=length(C2);
A=zeros(n,m);
parfor i=1:n
for j=1:m
A(i,j)=2*i-3*j;
end
end
[max_num,max_idx] = max(A(:)); %finding maximum element
[X Y]=ind2sub(size(A),max_num); %retrieving location of max element
% Need to use these values
bestC=C1(X)
bestD=C2(Y)
poolobj = gcp('nocreate');
delete(poolobj);
如果你想要的只是 A
的最小值,你不需要存储所有元素 - parfor
理解像 min
这样的缩减,所以像这样作品:
A = Inf;
parfor i=1:M
for j=1:N
A = min(A, rand);
end
end
好的,如果你想要极值和位置,你需要做更多的工作。不过,您不需要存储整个 A
,您仍然可以将其表述为 parfor
缩减。
function Axy = pfeg
% Axy contains the running maximum value of A, and the location
% where it was found. Initialize it with default values:
Axy = {-Inf, -1, -1};
parfor i=1:10
for j=1:10
% calculate the new value of "A(i,j)"
tmp = rand();
% Update the running maximum
Axy = iReduce(Axy, {tmp, i, j});
end
end
end
function axy = iReduce(axy, candidate)
if candidate{1} > axy{1}
axy = candidate;
end
end
我需要在 MATLAB 中使用 parfor
为外部循环编写一个简单的嵌套 for 循环。骨架代码为:
parfor i=1:M
for j=1:N
A(i,j)=myFunction(i,j);
end
end
在此之后,我需要找到矩阵A
中的最大值(对应的行号和列号)。但是,这个变量在 parfor
循环之外是不可访问的。最简单的方法是什么?这是分类器的多参数调整所必需的。
更新
这里是确切的代码:
C1=[0.001;100]; C2=[0.001;100];
A=zeros(length(C1),length(C2));
parfor i=1:length(C1)
for j=1:length(C2)
A(i,j)=2*i-3*j;
end
end
[max_num,max_idx] = max(A(:)); %finding maximum element
[X Y]=ind2sub(size(A),max_num); %retrieving location of max element
% Need to use these values
bestC=C1(X)
bestD=C2(Y)
poolobj = gcp('nocreate');
delete(poolobj);
这给出了错误:
Error: The variable A in a parfor cannot be classified.
稍作修改,Matlab 能够理解您的代码。
C1=[0.001;100]; C2=[0.001;100];
n=length(C1);
m=length(C2);
A=zeros(n,m);
parfor i=1:n
for j=1:m
A(i,j)=2*i-3*j;
end
end
[max_num,max_idx] = max(A(:)); %finding maximum element
[X Y]=ind2sub(size(A),max_num); %retrieving location of max element
% Need to use these values
bestC=C1(X)
bestD=C2(Y)
poolobj = gcp('nocreate');
delete(poolobj);
如果你想要的只是 A
的最小值,你不需要存储所有元素 - parfor
理解像 min
这样的缩减,所以像这样作品:
A = Inf;
parfor i=1:M
for j=1:N
A = min(A, rand);
end
end
好的,如果你想要极值和位置,你需要做更多的工作。不过,您不需要存储整个 A
,您仍然可以将其表述为 parfor
缩减。
function Axy = pfeg
% Axy contains the running maximum value of A, and the location
% where it was found. Initialize it with default values:
Axy = {-Inf, -1, -1};
parfor i=1:10
for j=1:10
% calculate the new value of "A(i,j)"
tmp = rand();
% Update the running maximum
Axy = iReduce(Axy, {tmp, i, j});
end
end
end
function axy = iReduce(axy, candidate)
if candidate{1} > axy{1}
axy = candidate;
end
end