How to use a nested loop to do a matrix calculation?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
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.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!