0

我正在尝试在 MATLAB 中构建句柄类对象的分布式数组,并且我想从此向量中提取特定句柄以在命令行上使用。鉴于下面的代码,我希望执行以下代码。我在两个实验室(matlabpool 2)上运行这个。getValue 函数是我需要帮助的,谢谢...

vec = buildArray; 
h = getValue(vec,6);
h.id

ClassA.m:我想并行分发的类。

classdef ClassA < handle
    properties
        id;
    end

    methods
       function obj = ClassA(id)
        obj.id = id;
       end
    end   
end

buildArray.m:从 ClassA 的本地实例构建 codistributed 数组的函数。

function vec = buildArray
X(:,1) = 1:10;              % create ids
gsize = size(X);            % the global size
X = distributed(X);         % distribute the ids

spmd    
    x = getLocalPart(X);       % extract the local ids
    local = cell(length(x),1); % create local storage for handles

    % Create the class instances
    for i = 1:length(x);     
        local{i} = ClassA(x(i));    
    end

    % Build the codistributed array of handles
    codist = codistributor1d(codistributor1d.unsetDimension, ...
    codistributor1d.unsetPartition, gsize);
    vec = codistributed.build(local,codist);
end

getValue.m这是我需要帮助的函数,目前它只显示包含具有指定 id 的类的实验室。我希望它返回句柄,以便可以从命令行使用它。这是怎么做到的?

function h = getValue(vec, id)
h = []; % just so it will not throw an error
spmd
 local = getLocalPart(vec);
 for i = 1:length(local);
    if local{i}.id == id;
        % Export h here
        disp(['ID ', num2str(id), ' is on lab ', num2str(labindex)]);
        break;
    end
 end 
end
4

1 回答 1

0

我能够使用它为 getValue.m 解决我的问题,有没有更好的方法?

function h = getValue(vec, id)

spmd  
local = getLocalPart(vec);
idx = [];
for i = 1:length(local);
    if local{i}.id == id;
        idx = i;
        break;
    end
end 

codist = codistributor1d(codistributor1d.unsetDimension, ...
    codistributor1d.unsetPartition, [numlabs,1]);
IDX = codistributed.build(idx, codist); 
end

c = gather(vec(IDX));
h = c{1};
于 2013-02-15T20:08:13.413 回答