Submatrix diagonal normalization without loop

3 views (last 30 days)
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 Comments
Matt J
Matt J on 14 Apr 2023
There would be no point to doing it without a for-loop. The data is super small.
John D'Errico
John D'Errico on 14 Apr 2023
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.

Sign in to comment.

Answers (1)

Rahul
Rahul on 9 Sep 2024
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!

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!