0

I have a matrix X with 3 columns. For the porous of the question X=randn(5,3). I want to normalize the columns of X S.T. each column will have a 0 mean and a 1 std. I was using the following code:

X=(X-mean(X))./std(X);

I am getting an std of 1. My mean, however, is a very small value close to 0 but not essential 0. I tried playing a bit with the numbers to find an explanation:

X=1:15;
X=reshape(X,[5 3]);
mean(X-mean(X));

Which gives me 0 value for each column.

X=1:15;
X=reshape(X,[5 3]);
mean((X-mean(X))./std(X));

Which does not. But 0/anything is still 0. what am I missing?

  • Why am I not getting 0 values?
  • Are the values I am getting good enough for a pre-clustering algorithm normalization?
havakok
  • 1,185
  • 2
  • 13
  • 45
  • Are you sure you've written the code you're using correctly? Copying what you've got gives a matrix mismatch error... Mean returns the average for each row, so `X-mean(X)` is not valid... – Philip Feb 18 '17 at 10:01
  • Check if there are local files implementing `mean` and `std` functions. – Rotem Feb 18 '17 at 10:08
  • @Philip I am sure about the code. I just copy and pasted to check. `mean(X)` gives a row with the mean for each column. `X-mean(X)` subtracts the mean of each column from each column. – havakok Feb 18 '17 at 10:08
  • @Rotem do you mean other then the original matlab implantation? I am sure there are none. – havakok Feb 18 '17 at 10:10
  • The code: `X=1:15;X=reshape(X,[5 3]);mean(X-mean(X));` gives an error. Are you sure you are using Matlab? Use `clear all`, and check again. – Rotem Feb 18 '17 at 10:13
  • @Rotem, just run it again (third time), copy paste from the post. I am getting no errors. – havakok Feb 18 '17 at 10:22

1 Answers1

3

Here is a version that does what I think you're trying to do... you need to replicate the matrix because X-mean(X) isn't valid (if you're using the standard implementation)-- you can't subtract a 1x3 from a 5x3.

r = 5; c = 3;
X=randn(r,c);
Xm=repmat(mean(X),r,1);
Xstd = repmat(std(X),r,1);
Xn = (X-Xm)./Xstd;
mean(Xn)
std(Xn)

For me this prints out

ans =

  1.0e-16 *

 -0.6661         0    0.4441


ans =

  1.0000    1.0000    1.0000

Which seems like exactly what you're looking for... note the 1e-16 multiplier on the mean values... this is essentially 0, with some floating point error.

Philip
  • 1,526
  • 1
  • 14
  • 25
  • I can and have subtract a 1x3 from a 5x3 (I have tested vry carfully for the correct values). I am getting the same values you are getting with my code. Maybe it is a derivative of MATLAB versions difference.In any case, my question was referring to your 'essentially 0' comment. Can you kindly elaborate on why are we not getting definite zeros? – havakok Feb 18 '17 at 10:20
  • 1
    Hm, you're definitely using a non-standard implementation if that works for you... in any case, floating point numbers are not infinitely precise. Check [here](http://stackoverflow.com/questions/588004/is-floating-point-math-broken), [here](http://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples), and [here](http://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) for elaboration. Or just google "floating point arithmetic error" – Philip Feb 18 '17 at 10:24
  • The difference might be due to one of the 'computer vision' expectations? I know for sure I\no one else have never implemented no `mean()` or subtraction function on this mu chine. – havakok Feb 18 '17 at 10:27
  • 1
    Oh, apparently matlab 2016b adds the capability to subtract compatible sizes--I'm still on 2015! – Philip Feb 18 '17 at 10:48