0

MATLAB でハンドル クラス オブジェクトの分散配列を構築しようとしています。コマンド ラインで使用するために、このベクトルから特定のハンドルを抽出したいと考えています。以下のコードを考えると、以下を実行したいと思います。これを 2 つのラボ (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 のローカル インスタンスから対話型分散配列を構築する関数。

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 に答える