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 个评论
Matt J
Matt J 2023-4-14
There would be no point to doing it without a for-loop. The data is super small.
John D'Errico
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
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!

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by