MATLAB Answers

# Creating a "triangular" matrix

2 views (last 30 days)
Radhika Kulkarni on 9 Feb 2021
Commented: Walter Roberson on 9 Feb 2021
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 CommentsShowHide 1 older comment
Radhika Kulkarni on 9 Feb 2021
Can it be done if there are 0's in the empty cells?

Sign in to comment.

### Answers (1)

Walter Roberson on 9 Feb 2021
B = reshape([A;zeros(size(A))], size(A, 1),[]);
B(1:2:end, 2:end) = B(1:2:end, 1:end-1) ;
##### 2 CommentsShowHide 1 older comment
Walter Roberson on 9 Feb 2021
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;

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!