Creating a "triangular" matrix

4 ビュー (過去 30 日間)
Radhika Kulkarni
Radhika Kulkarni 2021 年 2 月 9 日
コメント済み: Walter Roberson 2021 年 2 月 9 日
Hello,
I have a 17 x 75 matrix A, and have created a copy of the same matrix (B) for making changes. I want to manipulate this matrix by adding a column of 0's in between each column now. Furthermore, I want to shift the numbers in the ODD rows to the right by 1. In the end, I will have a "triangular" matrix. I have added a picture for visual reference.
Any help would be appreciated.
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 9 日
Numeric matrices can never have empty holes.
Radhika Kulkarni
Radhika Kulkarni 2021 年 2 月 9 日
Can it be done if there are 0's in the empty cells?

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

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 2 月 9 日
B = reshape([A;zeros(size(A))], size(A, 1),[]);
B(1:2:end, 2:end) = B(1:2:end, 1:end-1) ;
  2 件のコメント
Radhika Kulkarni
Radhika Kulkarni 2021 年 2 月 9 日
Hi Walter, thank you for your quick response! May I know why the numbers repeat on the first and and second column for the ODD rows? Is there a way to have the first number be 0 so there aren't repeats?
Walter Roberson
Walter Roberson 2021 年 2 月 9 日
Oh, right, I forgot to account for that.
B = reshape([A;zeros(size(A))], size(A, 1),[]);
B(1:2:end, 2:end) = B(1:2:end, 1:end-1);
B(1:2:end, 1) = 0;

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by