Search code examples
octavevectorization

Vectorization of feature scaling


I want to feature scale a matrix (X) with 2 columns. I am using mean normalization, and I wrote the following lines in Octave:

X_norm = X
mu = mean(X);
sigma = std(X);
X_norm(:,1) = (X_norm(:,1) .- mu(:,1)) ./ sigma(:,1);
X_norm(:,2) = (X_norm(:,2) .- mu(:,2)) ./ sigma(:,2); 

Can you please let me know a cleaner way to vectorize these calculation?

I checked my code by comparing with the result from zscore(X) and they matched - i.e. a sum(X_norm - zscore(X)) returned me 0 0.

I am constrained to not use zscore(), and hence the question.

Sample data as follows:

2104      3
1600      3
2400      3
1416      2
3000      4
1985      4
1534      3
1427      3
1380      3
1494      3
1940      4
2000      3
1890      3
4478      5
1268      3
2300      4
1320      2
1236      3
2609      4
3031      4
1767      3
1888      2
1604      3
1962      4
3890      3
1100      3
1458      3
2526      3
2200      3
2637      3

Solution

  • You could simply do:

    X_norm = (X .- mean(X,1)) ./ std(X,0,1);