2013-02-15 86 views
0

我正在嘗試在MATLAB中構建句柄類對象的分佈式數組,我想從該矢量中提取特定的句柄以便在命令行上使用。鑑於下面的代碼,我希望執行以下內容。我正在用兩個實驗(matlabpool 2)運行這個。該getValue函數是什麼,我需要,謝謝幫助...從MATLAB公共數組提取值

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 

回答

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};