Replacing for loops with matrix math.

1 vue (au cours des 30 derniers jours)
Douglas Brenner
Douglas Brenner le 12 Oct 2018
Modifié(e) : Matt J le 12 Oct 2018
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);
end
end
  1 commentaire
madhan ravi
madhan ravi le 12 Oct 2018
undefined datas??

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 12 Oct 2018
Modifié(e) : Matt J le 12 Oct 2018
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.
[m,n]=size(diag);
[I,J]=ndgrid(1:m,1:n);
keep= J>=I & J<=num_pts-I;
I=I(keep); J=J(keep);
diag(keep) = testm1(J,2) .* testm2(J+I,2);

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by