Submatrix diagonal normalization without loop
3 次查看(过去 30 天)
显示 更早的评论
Imagine we have matrix A, 8 by 8. Is there any way to nomalize each 4 by 4 submatrix by each corresponding diagonal entries but without for_loop. any signal.proc toolbox?
2 个评论
John D'Errico
2023-4-14
For an 8x8 matrix, yes, you could reshape and permute the matrix, getting it into 4 blocks. Then you could do as you wish to each block, and finally, reconstitute the normalized matrix. By the time you were done, the result would bo convoluted code you could not read, nor remember how it works when you need to maintain it. And it would be no faster.
Just use a loop.
回答(1 个)
Rahul
2024-9-9
I understand that you have an 8x8 Matrix and wish to normalize each 4x4 Submatrix by each corresponding diagonal entries without a loop.
You can consider solving this problem with the help of 'diag' function as it helps in extracting the 4x4 Submatrices and then applying diagonal normalization to those matrices in the following way:
A = rand(8); % Example 8x8 matrix
% Extract diagonal elements for each 4x4 block
D1 = diag(A(1:4, 1:4));
D2 = diag(A(1:4, 5:8));
D3 = diag(A(5:8, 1:4));
D4 = diag(A(5:8, 5:8));
% Normalization matrices for each block
N1 = diag(1 ./ D1);
N2 = diag(1 ./ D2);
N3 = diag(1 ./ D3);
N4 = diag(1 ./ D4);
% Normalize each 4x4 submatrix
A(1:4, 1:4) = N1 * A(1:4, 1:4);
A(1:4, 5:8) = N2 * A(1:4, 5:8);
A(5:8, 1:4) = N3 * A(5:8, 1:4);
A(5:8, 5:8) = N4 * A(5:8, 5:8);
%% Here we obtain 'A' as the required matrix where each 4x4 submatrix is normalized by it's diagonal elements.
You can refer to this MATLAB documenatation to know more about the 'diag' function: https://www.mathworks.com/help/releases/R2024a/matlab/ref/diag.html?searchHighlight=diag&s_tid=doc_srchtitle
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!