How to use a nested loop to do a matrix calculation?

1 vue (au cours des 30 derniers jours)
Zhsha
Zhsha le 16 Sep 2019
Modifié(e) : madhan ravi le 16 Sep 2019
For example, i defined a new function called d(x,y).
The x matrix would be [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
and the y matrix would be [16 15 14 13; 13 14 15 16; 17 18 19 20; 21 22 22 23].
How can I get the result with the function d(x,y) with a nested loop?
The result matrix should have the same size as x and y matrix.
Thank you guys

Réponse acceptée

madhan ravi
madhan ravi le 16 Sep 2019
Modifié(e) : madhan ravi le 16 Sep 2019
x = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
y = [16 15 14 13; 13 14 15 16; 17 18 19 20; 21 22 22 23]
v = zeros(k,l);
for k = 1:numel(x)
for l = 1:numel(y)
v(k,l)= d(x(k),y(l));
end
end
Note: The same can be achieved using meshgrid() or ndgrid(). I haven’t see the function d() if it’s properly vectorised you can take advantage of implicit expansion.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by