我有以下代碼在matlab上做了一些排序,但依賴於eval。這是一組更大的代碼的一部分,我已經簡化了它能夠放在這裏。基本上,我正在尋找一種擺脫函數eval的簡單方法,以便我可以在代碼中使用parfor語句。透明度 - 在matlab中替換EVAL
t = 1;
N = 1500;
BM = rand(N,1);
P1 = rand(N,12);
nport = 10;
tSpan = t : t + 11;
pointer = round([1; N*.10; N*.20; N*.30; N*.40;N*.50; N*.60; N*.70; N*.80; N*.90; N]); % pointers used in sorting
IndStru = struct('idp1', [], 'idp2', [], 'idp3', [], 'idp4', [], 'idp5',[],'idp6', [], 'idp7', [], 'idp8', [], 'idp9', [], 'idp10',[]);
[ssize, sInd] = sort(BM); clear ssize
ids1 = sInd(pointer(1) : pointer(2));
ids2 = sInd(pointer(2) + 1 : pointer(3));
ids3 = sInd(pointer(3) + 1 : pointer(4));
ids4 = sInd(pointer(4) + 1 : pointer(5));
ids5 = sInd(pointer(5) + 1 : pointer(6));
ids6 = sInd(pointer(6) + 1 : pointer(7));
ids7 = sInd(pointer(7) + 1 : pointer(8));
ids8 = sInd(pointer(8) + 1 : pointer(9));
ids9 = sInd(pointer(9) + 1 : pointer(10));
ids10 = sInd(pointer(10) + 1 : pointer(11)); clear sInd
for i = 1 : nport
eval(['IndStru.idp' num2str(i) '=ids' num2str(i) ';' ]);
eval(['p10(' num2str(i) ',' num2str(tSpan(1)) ':' num2str(tSpan(12)) ') = sum(P1(IndStru.idp' num2str(i) ', tSpan))']);
end
你的例子不起作用,特別是第24行,加上缺少'for'。至於第一個'eval',看看[從變量生成字段名稱](http://se.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html),第二個可以只是拼寫出來,不需要字符串。 –
謝謝。在爲我的MWE轉換我的代碼時出現了輸入錯誤。現在應該沒問題。我會看看你發佈的鏈接。 – phdstudent
其實,我認爲你對此有錯誤的看法?編號字段的結構看起來很奇怪。你想要解決什麼問題。 –