MATLAB Answers

0

split a rectangular matrix

Asked by Elysi Cochin on 1 Apr 2019
Latest activity Commented on by Akira Agata
on 1 Apr 2019
i have a matrix as
input = [1 0 0 0; 0 1 0 0; 0 0 1 0];
how can i split a the input matrix as
output1 = [1 0 0 0; 0 0 0 0; 0 0 0 0];
output2 = [0 0 0 0; 0 1 0 0; 0 0 0 0];
output3 = [0 0 0 0; 0 0 0 0; 0 1 0 0];
store output1-3 in a single matrix named output with n-dimension.
matrix input can be of any dimension but rectangular matrix and only diagonal values will be there.

  0 Comments

Sign in to comment.

1 Answer

Answer by Akira Agata
on 1 Apr 2019
 Accepted Answer

How about the following?
input = [1 0 0 0; 0 1 0 0; 0 0 1 0];
output = zeros([size(input),size(input,1)]);
for kk = 1:size(input,1)
output(kk,:,kk) = input(kk,:);
end

  2 Comments

Note: Naming a variable input is not a good idea, it will shadow the in-built function input().
Yes, that's true. Thank you for your additional comment !

Sign in to comment.