MATLAB: How to generate a identity matrix based on another matrix

identity matrix

Hi, all,
Here is my problem, there is a matrix [1 2 3; 2 3 4],
How to change it into the following matrix
[1 2 3 0 0 0; 0 0 0 2 3 4];
Thanks a lot.

Best Answer

You can use blkdiag for this:
>> A = [1 2 3; 2 3 4];
>> B = num2cell(A,2);
>> blkdiag(B{:})
ans =
1 2 3 0 0 0
0 0 0 2 3 4
This is not an identity matrix though.