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

1 visualización (últimos 30 días)
Zhsha
Zhsha el 16 de Sept. de 2019
Editada: madhan ravi el 16 de Sept. de 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

Respuesta aceptada

madhan ravi
madhan ravi el 16 de Sept. de 2019
Editada: madhan ravi el 16 de Sept. de 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.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by