0
votes

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?

1
I would advice to include some code you have tried into your question. It will increase the chances of getting an answer and reduce the chances of having your question flagged and removed. - acarlstein

1 Answers

2
votes

Let

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

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

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