I need to generate an array where the elements in the array are the sum of the indices... I am quite lost with the nested for loop aspect... please assist if possible.

1 回表示 (過去 30 日間)
% If m = 12, and n = 13 then the final solution should be a 12 x 13 matrix with the min value being 2 and max value being 25
function A = MagicArrayAdd(m,n)
N = zeros(m,n)
[row col] = size(N)
A = 1
for i = 1:row
for j = i:col
end
end
end

採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 9 月 28 日
編集済み: KALYAN ACHARJYA 2019 年 9 月 28 日
% If m = 12, and n = 13 then the final solution should be a 12 x 13 matrix with the min value being 2 and max value being 25
function A=MagicArrayAdd(m,n)
N=zeros(m,n);
for i=1:m
for j=1:n
N(i,j)=i+j;
end
end
N
end
You are doing using loops (I am quite lost with the nested for loop aspect), same can be done without loop also. Recomended
Result:
>> MagicArrayAdd(5,5)
N =
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
  2 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 9 月 28 日
編集済み: KALYAN ACHARJYA 2019 年 9 月 28 日
Tiny mistake..hahaha:: It took me 3 minutes to get the "i" error
Error is here
for j = i:col
%....^i here>> must be 1
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by