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

2 ビュー (過去 30 日間)
Zhsha
Zhsha 2019 年 9 月 16 日
編集済み: madhan ravi 2019 年 9 月 16 日
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

採用された回答

madhan ravi
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 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by