4
我一直在使用bsxfun工作向量化我的代碼,但是我遇到了一個我無法完全破解的場景。這是一個小問題的例子。我想刪除這段代碼中的for循環,但是我對tempEA行很困難。MATLAB bsxfun或矢量化
Index = [2; 3; 4;];
dTime = [25; 26; 27; 28; 25; 26; 27; 28; 27; 28];
dIndex = [3; 3; 3; 2; 1; 3; 2; 4; 4; 2];
aTime = [30; 38; 34; 39; 30; 38; 34; 39; 34; 39];
aIndex = [4; 2; 5; 4; 5; 4; 4; 2; 2; 4];
EA = zeros(numel(Index));
for i = 1:numel(Index)
for j = 1:numel(Index)
tempEA = aTime(Index(i) == dIndex(:,1) & Index(j) == aIndex(:,1));
if i == j
elseif tempEA > 0
EA(i,j) = min(tempEA);
else
EA(i,j) = 50;
end
end
end
答案應該是這樣的:
EA =
0 50 34
38 0 30
34 50 0
感謝您的幫助提前。
很好地完成。我喜歡代碼中的評論。非常感謝。 –