简体   繁体   中英

Create a special diagonal matrix

I want to create a special diagonal matrix in MATLAB that uses [1 1] or any other array as main diagonal elements. Something like the following:

[1   1   0   0   0   0
 0   0   1   1   0   0
 0   0   0   0   1   1]

How can I do that without using any loop structures?

Let

v = [1 1];
n = 3;
  • Using kron :

     result = kron(eye(n), v); 
  • Using blkdiag :

     vv = repmat({v}, 1, n); result = blkdiag(vv{:}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM