MATLAB: Adding parts of matrix to another matrix

concatenatingmatrix

I have a matrix X = [1 2 3 4 5] and matrix Z = [6 7 8 9 10; 11 12 13 14 15]. I would like to get a matrix A that looks like this [6 7 8 9 10; 11 12 13 14 15; 1 1 1 1 1; 0 2 2 2 2; 0 0 3 3 3; 0 0 0 4 4;0 0 0 0 5]. I've looked into multiple ways of concatenating matrices, for example horzcat and vertcat, but this doesn't seem very helpful for this problem. I would imagine writing some loop to do this and adding the elements one by one, but i can't come up with a solution.

Best Answer

X = [1 2 3 4 5];
Z = [6 7 8 9 10; 11 12 13 14 15];
[Z; triu(repmat(X.', 1, numel(X)))]