Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast technique for normalizing a matrix in MATLAB

I want to normalise each column of a matrix in Matlab. I have tried two implementations:

Option A:

mx=max(x);
mn=min(x);
mmd=mx-mn;
for i=1:size(x,1)
    xn(i,:)=((x(i,:)-mn+(mmd==0))./(mmd+(mmd==0)*2))*2-1; 
end

Option B:

mn=mean(x);
sdx=std(x);
for i=1:size(x,1)
    xn(i,:)=(x(i,:)-mn)./(sdx+(sdx==0));
end

However, these options take too much time for my data, e.g. 3-4 seconds on a 5000x53 matrix. Thus, is there any better solution?

like image 789
Ariyan Avatar asked Dec 23 '10 18:12

Ariyan


People also ask

How do you normalize a matrix in Matlab?

N = normalize( A ) returns the vectorwise z-score of the data in A with center 0 and standard deviation 1. If A is a vector, then normalize operates on the entire vector A . If A is a matrix, then normalize operates on each column of A separately.

How do you normalize a matrix?

For normalization, the calculation follows as subtracting each element by minimum value of matrix and thereby dividing the whole with difference of minimum and maximum of whole matrix.

What is the best normalization method?

Best Data Normalization Techniques In my opinion, the best normalization technique is linear normalization (max – min). It's by far the easiest, most flexible, and most intuitive.

What are Normalisation techniques?

Four common normalization techniques may be useful: scaling to a range. clipping. log scaling. z-score.


1 Answers

Remember, in MATLAB, vectorizing = speed.

If A is an M x N matrix,

A = rand(m,n);
minA = repmat(min(A), [size(A, 1), 1]);
normA = max(A) - min(A);               % this is a vector
normA = repmat(normA, [length(normA) 1]);  % this makes it a matrix
                                       % of the same size as A
normalizedA = (A - minA)./normA;  % your normalized matrix
like image 199
eykanal Avatar answered Sep 27 '22 02:09

eykanal