Adding every loop a matrix from the right side to a matrix

3 ビュー (過去 30 日間)
Lars Urban
Lars Urban 2022 年 5 月 10 日
コメント済み: KSSV 2022 年 5 月 10 日
I want to add after one loop a matrix to a matrix that was calculated one loop before. For example in the first loop the matrix would be 2x2 after second loop 2x4 and the next one 2x6 ...
I know that "manually" it would be i think like this
A = [1,2;3,4]
A = 2×2
1 2 3 4
B(:,1:2) = A
B = 2×2
1 2 3 4
B(:,3:4) = A
B = 2×4
1 2 1 2 3 4 3 4
But how would this look like in a loop?

採用された回答

KSSV
KSSV 2022 年 5 月 10 日
A = [1,2;3,4] ;
B = repmat(A,1,2)
B = 2×4
1 2 1 2 3 4 3 4
  4 件のコメント
Lars Urban
Lars Urban 2022 年 5 月 10 日
Sorry the problem why it was creating a bigger matrix after a loop was because i had a mistake before repmat. Problem solved thank you for the help :)
KSSV
KSSV 2022 年 5 月 10 日
Glad it worked for you.
Happy coding in MATLAB. :)

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

その他の回答 (1 件)

Davide Masiello
Davide Masiello 2022 年 5 月 10 日
編集済み: Davide Masiello 2022 年 5 月 10 日
In the example below, a new 2x2 random matrix is created at each new iteration and appended to an existing matrix A.
clear,clc
A = [];
for i = 1:10
B = rand(2,2);
A = [A,B];
end
A
A = 2×20
0.3994 0.6342 0.9210 0.0792 0.5605 0.1604 0.5920 0.6461 0.9130 0.8542 0.8578 0.3106 0.6250 0.0873 0.7464 0.4444 0.7945 0.1335 0.2230 0.5452 0.6890 0.5618 0.7523 0.2968 0.8271 0.6431 0.2080 0.2957 0.9066 0.7982 0.6292 0.7462 0.0664 0.8576 0.5080 0.5076 0.3264 0.3242 0.8307 0.2477
  1 件のコメント
Lars Urban
Lars Urban 2022 年 5 月 10 日
Thank you but i will stick with the repmat mehtod:)

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

カテゴリ

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