MATLAB Answers

Replacing for loops with matrix math.

8 ビュー (過去 30 日間)
Douglas Brenner
Douglas Brenner 2018 年 10 月 12 日
編集済み: Matt J 2018 年 10 月 12 日
How can I simplify this code. I.e remove the loops and replace them with matrix math.
for i=1:3
for j = i:num_pts - i
diag(i,j) = testm1(j,2) * testm2(j+i,2);
  1 件のコメント
madhan ravi
madhan ravi 2018 年 10 月 12 日
undefined datas??



Matt J
Matt J 2018 年 10 月 12 日
編集済み: Matt J 2018 年 10 月 12 日
I wouldn't say that replacing the loops with matrix operations is a way to "simplify" it, but here's how you could do it.
keep= J>=I & J<=num_pts-I;
I=I(keep); J=J(keep);
diag(keep) = testm1(J,2) .* testm2(J+I,2);

その他の回答 (0 件)


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by