How to use a nested loop to do a matrix calculation?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
madhan ravi
2019-9-16
编辑:madhan ravi
2019-9-16
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!