Create a matrix of this type?
Show older comments
Hello,
I want to make a matrix this type
1 0 0 0 0 0
2 0 0 0 0 0
3 4 0 0 0 0
5 6 0 0 0 0
7 8 9 0 0 0
10 11 12 0 0 0
13 14 15 16 0 0
17 18 19 20 0 0
% Alternate rows have same number of element
%Each element of the matrix is 1 larger than previous one
How to achieve it ?
Accepted Answer
More Answers (1)
madhan ravi
on 20 Jan 2019
Edited: madhan ravi
on 20 Jan 2019
n=6; % number of elements in a row
B=mat2cell((1:20).',repelem(1:4,2));
B=cellfun( @transpose,B,'un',0);
R=cellfun( @(x) [x zeros(1,6-numel(x))],B,'un',0);
vertcat(R{:})
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!