-2

I have a matrix D = zeros (30, 432); i want to assign d = [ 1 1 0 0; 0 0 1 1; 0 0 0 0]; to the diagonals of matrix D. i have the code below but it doesn't allow me to assign d for every diagonal values in D.

[N,~,P,Q]=size(D);
diagIndex=repmat(logical(eye(N)),[1 1 P Q]);
D(diagIndex)=d;

The output for 30x432 matrix would be like :

d 0 0 0
0 d 0 0
0 0 d 0
0 0 0 d
lighting
  • 400
  • 2
  • 13

1 Answers1

0

You can use spdiags to create a diagonal [10 x 108] sparse matrix then use kron to scale and fill the matrix.

d = [ 1 1 0 0; 0 0 1 1; 0 0 0 0]
size_D=[30, 432];
sz = size_D./size(d);
diagonal = spdiags(ones(sz(1),1),0,sz(1),sz(2));
result = kron(diagonal ,d);
rahnema1
  • 15,264
  • 3
  • 15
  • 27