How can we insert a row matrix without altering the rest of the values?

1 回表示 (過去 30 日間)
Ashfaq Ahmed
Ashfaq Ahmed 2022 年 12 月 2 日
コメント済み: Ashfaq Ahmed 2022 年 12 月 2 日
Hi!
Say, I have this matrix -
A =
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
Now I want to add A*10 after each of the rows so that I can make this matrix -
A =
1 2 3
10 20 30
4 5 6
40 50 60
7 8 9
70 80 90
10 11 12
100 110 120
13 14 15
130 140 150
I have hundreds of rows, so I would prefer a loop (or anything!)
Thank you for your help.

採用された回答

Dyuman Joshi
Dyuman Joshi 2022 年 12 月 2 日
編集済み: Dyuman Joshi 2022 年 12 月 2 日
A = reshape(1:15,3,5)'
A = 5×3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
%pre-allocation
y=zeros(size(A,1)*2,size(A,2));
y(1:2:end,:)=A;
y(2:2:end,:)=A*10
y = 10×3
1 2 3 10 20 30 4 5 6 40 50 60 7 8 9 70 80 90 10 11 12 100 110 120 13 14 15 130 140 150
  1 件のコメント
Ashfaq Ahmed
Ashfaq Ahmed 2022 年 12 月 2 日
You are very intelligent! The algorithm is awesome!

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

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2022 年 12 月 2 日
編集済み: Fangjun Jiang 2022 年 12 月 2 日
A=magic(3);
C=transpose(reshape([A,10*A]',size(A,1),[]))
C = 6×3
8 1 6 80 10 60 3 5 7 30 50 70 4 9 2 40 90 20

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by