Iteratively remove the rows and columns of a matrix

5 views (last 30 days)
I have a matrix H, which obtains the strcuture as shown below. H is block sparse, where each color block denotes non-empty block and white color denote block with all zeros. Each row and column of H has N blocks. Each block is of size M \times M.
Now I have another matrix C, which is obtained via first vectorize H denoted by h=vec(H), then C=h*h^H, where ^H denotes Hermitian operation. So actually C is a sample covariance matrix. My goal is to densify the matrix C. Densify means I want to remove all zeros rows and columns of C based on the structure H or equivalently h.
My current question is I have loop to find the index where h are non zero. For example, for the first part of h, which in the range 1:NM of h, 1: 2M are non-zero and 2M:(N-2)M is empty,. Then I remove all the rows of C from 2M:(N-2)M, and do the same for columns of C as well. However, after removing the zeros rows and columns in C in this iteration, it becomes difficult for me to find the proper index as the size of C has changed in last iteration.
Therefore, is there any convenient way to implmenet the matrix C densify?
Note that densify h or H then compute C can not be done due to the other parts limits of my project.

Answers (2)

Matt J
Matt J on 13 Apr 2025
Hnz=nonzeros(H);
C=Hnz*Hnz';

Chuguang Pan
Chuguang Pan on 13 Apr 2025
Edited: Chuguang Pan on 13 Apr 2025
M = 3;
N = 8;
[H00,H10,H20,H30,H40,H50,H60,H70] = deal(randi(10,M));
[H11,H21,H31,H41,H51,H61,H71] = deal(randi(20,M));
H = blkdiag(H00,H10,H20,H30,H40,H50,H60,H70) + [zeros(1*M,N*M);blkdiag(H11,H21,H31,H41,H51,H61,H71) zeros((N-1)*M,1*M)];
h = H(:);
C = h*h';
nnzIdx = find(h);
nnzH = C(nnzIdx,nnzIdx)
nnzH = 135×135
36 18 12 60 54 90 36 36 36 48 72 36 12 36 36 18 54 114 36 18 12 60 54 90 36 36 36 48 72 36 18 9 6 30 27 45 18 18 18 24 36 18 6 18 18 9 27 57 18 9 6 30 27 45 18 18 18 24 36 18 12 6 4 20 18 30 12 12 12 16 24 12 4 12 12 6 18 38 12 6 4 20 18 30 12 12 12 16 24 12 60 30 20 100 90 150 60 60 60 80 120 60 20 60 60 30 90 190 60 30 20 100 90 150 60 60 60 80 120 60 54 27 18 90 81 135 54 54 54 72 108 54 18 54 54 27 81 171 54 27 18 90 81 135 54 54 54 72 108 54 90 45 30 150 135 225 90 90 90 120 180 90 30 90 90 45 135 285 90 45 30 150 135 225 90 90 90 120 180 90 36 18 12 60 54 90 36 36 36 48 72 36 12 36 36 18 54 114 36 18 12 60 54 90 36 36 36 48 72 36 36 18 12 60 54 90 36 36 36 48 72 36 12 36 36 18 54 114 36 18 12 60 54 90 36 36 36 48 72 36 36 18 12 60 54 90 36 36 36 48 72 36 12 36 36 18 54 114 36 18 12 60 54 90 36 36 36 48 72 36 48 24 16 80 72 120 48 48 48 64 96 48 16 48 48 24 72 152 48 24 16 80 72 120 48 48 48 64 96 48
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 Comment
Chuguang Pan
Chuguang Pan on 13 Apr 2025
Edited: Chuguang Pan on 13 Apr 2025
@charrrrlie I use find function to extrat nonzero element index from sparse vector h and extract the nonzero elements from C matrix using nonzero element index.

Sign in to comment.

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!