Trying to normalize a matrix across all element values.

I have the triu of an 8x8 adjacency matrix A shown below. I would LIKE to normallize all the non-zero elements using normalize(A,'range'), for instance with output values between 0 and 1, but NOT column or row-wise - I'd like to normalize across ALL non-zero values. I haven't been able to find options for this. Any help appreciated!
[ 0 54 70 67 18 13 100 18
0 0 89 67 20 37 47 99
0 0 0 38 20 43 49 13
0 0 0 0 26 30 62 27
0 0 0 0 0 11 91 88
0 0 0 0 0 0 17 18
0 0 0 0 0 0 0 4
0 0 0 0 0 0 0 0 ]

 Accepted Answer

M=[ 0 54 70 67 18 13 100 18
0 0 89 67 20 37 47 99
0 0 0 38 20 43 49 13
0 0 0 0 26 30 62 27
0 0 0 0 0 11 91 88
0 0 0 0 0 0 17 18
0 0 0 0 0 0 0 4
0 0 0 0 0 0 0 0 ];
M(M~=0)=rescale(M(M~=0),0,1)
M = 8×8
0 0.5208 0.6875 0.6562 0.1458 0.0938 1.0000 0.1458 0 0 0.8854 0.6562 0.1667 0.3438 0.4479 0.9896 0 0 0 0.3542 0.1667 0.4062 0.4688 0.0938 0 0 0 0 0.2292 0.2708 0.6042 0.2396 0 0 0 0 0 0.0729 0.9062 0.8750 0 0 0 0 0 0 0.1354 0.1458 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

2 Comments

Ah! Excellent and elegant. Thank you!
Follow up Question - if I wanted to do something similar, but scaled to standard deviation (such as normallize(A, 'scale'), is there a similar approach?
Belay that...I think you've already answered it - namely:
A(A~=0)= normalize(A(A~=0),'scale')
Thanks again.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 27 Jan 2026 at 21:01
Edited: Matt J on 27 Jan 2026 at 21:06
Normalize across only non-zero elements, or across elements only in the upper triangle? If the latter, then,
A=[ 0 54 0 67 18 13 100 18
0 0 89 67 0 37 47 99
0 0 0 38 -1 43 49 13
0 0 0 0 26 30 62 27
0 0 0 0 0 11 91 88
0 0 0 0 0 0 17 18
0 0 0 0 0 0 0 4
0 0 0 0 0 0 0 0 ];
idx=triu(true(size(A)),1);
A(idx) = normalize(A(idx),'range')
A = 8×8
0 0.5446 0.0099 0.6733 0.1881 0.1386 1.0000 0.1881 0 0 0.8911 0.6733 0.0099 0.3762 0.4752 0.9901 0 0 0 0.3861 0 0.4356 0.4950 0.1386 0 0 0 0 0.2673 0.3069 0.6238 0.2772 0 0 0 0 0 0.1188 0.9109 0.8812 0 0 0 0 0 0 0.1782 0.1881 0 0 0 0 0 0 0 0.0495 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

3 Comments

Ah, this is very useful, too. I was about to ask a related question: In DB's solution, the zero scale then ended up zeroing out some "weights" in the adjacency matrix (which is not good). This solution keeps zero in the source matrix, so the low one (.0495) is still around (good).
My question is, do the 8 zeros change the scaling in any way compared to, say just a single zero? (I'm guessing not, but I'm a stats novice)
Matt J
Matt J on 27 Jan 2026 at 22:19
Edited: Matt J on 27 Jan 2026 at 22:20
I don't know what is meant by the "8 zeros". The scaling is determined by the min and max of the matrix subset A(idx), indepndently of how many zeros that contains.
dpb
dpb on 27 Jan 2026 at 22:32
Edited: dpb on 29 Jan 2026 at 17:01
"the zero scale then ended up zeroing out some "weights" in the adjacency matrix"
That's the solution you asked for -- "I would LIKE to normallize all the non-zero elements ...with output values between 0 and 1,"
What is supposed to be the minimum value then, if not zero? Specify it as the lower bound.

Sign in to comment.

Products

Release

R2025b

Asked:

on 27 Jan 2026 at 20:08

Edited:

dpb
on 29 Jan 2026 at 17:01

Community Treasure Hunt

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

Start Hunting!