how to put while loop output in array form

15 ビュー (過去 30 日間)
jeffery lamar
jeffery lamar 2021 年 8 月 8 日
編集済み: the cyclist 2021 年 8 月 8 日
Please, i want my while loop output to be in array form so every element can be accessible, the outpu should be in this form
2 8 4 5 2 11 6 5 8 5
5 11 7 8 5 14 9 8 11 8
6 12 8 9 6 15 10 9 12 9
11 17 13 14 11 20 15 14 17 14
10 16 12 13 10 19 14 13 16 13
4 10 6 7 4 13 8 7 10 7
3 9 5 6 3 12 7 6 9 6
7 13 9 10 7 16 11 10 13 10
9 15 11 12 9 18 13 12 15 12
5 11 7 8 5 14 9 8 11 8
A=[1 4 5 10 9 3 2 6 8 4];
B=[1 7 3 4 1 10 5 4 7 4];
i = length(A);
C = 0;
while(C < i)
C=C+1;
u=A(C)+B
end

採用された回答

the cyclist
the cyclist 2021 年 8 月 8 日
編集済み: the cyclist 2021 年 8 月 8 日
If you have a recent version of MATLAB, this will work:
A=[1 4 5 10 9 3 2 6 8 4];
B=[1 7 3 4 1 10 5 4 7 4];
out = A' + B
out = 10×10
2 8 4 5 2 11 6 5 8 5 5 11 7 8 5 14 9 8 11 8 6 12 8 9 6 15 10 9 12 9 11 17 13 14 11 20 15 14 17 14 10 16 12 13 10 19 14 13 16 13 4 10 6 7 4 13 8 7 10 7 3 9 5 6 3 12 7 6 9 6 7 13 9 10 7 16 11 10 13 10 9 15 11 12 9 18 13 12 15 12 5 11 7 8 5 14 9 8 11 8
This will use implicit expansion to get the result. If that syntax does not work, use the bsxfun instead:
out = bsxfun(@plus,A',B);

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 8 月 8 日
A=[1 4 5 10 9 3 2 6 8 4];
B=[1 7 3 4 1 10 5 4 7 4];
i = length(A);
C = 0;
while(C < i)
C=C+1;
u(C,:) = A(C) + B;
end
u
u = 10×10
2 8 4 5 2 11 6 5 8 5 5 11 7 8 5 14 9 8 11 8 6 12 8 9 6 15 10 9 12 9 11 17 13 14 11 20 15 14 17 14 10 16 12 13 10 19 14 13 16 13 4 10 6 7 4 13 8 7 10 7 3 9 5 6 3 12 7 6 9 6 7 13 9 10 7 16 11 10 13 10 9 15 11 12 9 18 13 12 15 12 5 11 7 8 5 14 9 8 11 8
Or... you could just use
A.' + B
ans = 10×10
2 8 4 5 2 11 6 5 8 5 5 11 7 8 5 14 9 8 11 8 6 12 8 9 6 15 10 9 12 9 11 17 13 14 11 20 15 14 17 14 10 16 12 13 10 19 14 13 16 13 4 10 6 7 4 13 8 7 10 7 3 9 5 6 3 12 7 6 9 6 7 13 9 10 7 16 11 10 13 10 9 15 11 12 9 18 13 12 15 12 5 11 7 8 5 14 9 8 11 8

カテゴリ

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