Gregor Isack
Gregor Isack

Reputation: 1141

Normalize each slice of a 3D matrix

How do I normalize each slice of a 3D matrix? I tried like this:

a=rand(1,100,3481);
a= (a - min(a)) ./ (max(a)-min(a)); % 

By right each slice of matrix should ranges from 0 to 1. But that is not the case, I don't find 1 in some of the slices. As I inspected, min(a) and max(a) returned the respective value in 3D. Thus it should be of no issue using the code above. Is there something I missed for 3D matrix? Thanks in advance!

Upvotes: 2

Views: 687

Answers (2)

Sami Varjo
Sami Varjo

Reputation: 128

My option would be without reshaping as it is sometimes bit difficult to understand. I use min max with the dimension you want want to use for normalization with repmat to clone...:

a=rand(1,100,3481);

a_min2 = min(a,[],2);
a_max2 = max(a,[],2);
a_norm2 = (a - repmat(a_min2,[1 size(a,2) 1]) ) ./ repmat( (a_max2-a_min2),[1 size(a,2) 1]);

or if normalization on 3rd dim...

a_min3 = min(a,[],3);
a_max3 = max(a,[],3);
a_norm3 = (a - repmat(a_min3,[1 1 size(a,3)]) ) ./ repmat( (a_max3-a_min3),[1 1 size(a,3)]);

Upvotes: 0

Divakar
Divakar

Reputation: 221704

We need to find the minimum and maximum values for each of those 2D slices and then we can use bsxfun to do those operations in a vectorized manner with help from permute to let the singleton dims align properly to let bsxfun do its broadcasting job (or use reshape there).

Hence, the implementation would be -

mins = min(reshape(a,[],size(a,3)));
maxs = max(reshape(a,[],size(a,3)));
a_offsetted = bsxfun(@minus, a, permute(mins,[1,3,2]));
a_normalized = bsxfun(@rdivide, a_offsetted, permute(maxs-mins,[1,3,2]))

Sample input, output -

>> a
a(:,:,1) =
     2     8     2     2
     8     3     8     2
a(:,:,2) =
     8     1     1     5
     4     9     8     6
a(:,:,3) =
     7     9     3     5
     6     2     6     5
a(:,:,4) =
     9     3     4     9
     7     1     9     9
>> a_normalized
a_normalized(:,:,1) =
         0    1.0000         0         0
    1.0000    0.1667    1.0000         0
a_normalized(:,:,2) =
    0.8750         0         0    0.5000
    0.3750    1.0000    0.8750    0.6250
a_normalized(:,:,3) =
    0.7143    1.0000    0.1429    0.4286
    0.5714         0    0.5714    0.4286
a_normalized(:,:,4) =
    1.0000    0.2500    0.3750    1.0000
    0.7500         0    1.0000    1.0000

Upvotes: 4

Related Questions