Submatrix diagonal normalization without loop

조회 수: 3 (최근 30일)
amin rodboard
amin rodboard 2023년 4월 14일
답변: Rahul 2024년 9월 9일
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