1

Hi I am new using MATLAB

What I am going to do is that I have m-dimensional samples(data), which m is 2 here.

Data in MATLAB code is like this

X1 = [4,2;2,4;2,3;3,6;4,4];
X2 = [9,10;6,8;9,5;8,7;10,8];

I am going to subtract average of samples, which is

Mu1 = [3;3.8];

Then can I subtract each 2D data with average of samples in MATLAB code in one line

2 Answers2

2

If I understand you correctly, you want to subtract the average Mu1 from your data (x1 and x2).

If so you could use the bsxfun function:

X1_subtracted = bsxfun(@minus, X1, Mu1')

which outputs:

X1_subtracted =
     1.0000   -1.8000
    -1.0000    0.2000
    -1.0000   -0.8000
          0    2.2000
     1.0000    0.2000

Note you are going to have to use ' with the Mu1 as X1 has the shape 5x2 while Mu1 has the shape 1x2.

Matt
  • 12,848
  • 2
  • 31
  • 53
kkawabat
  • 1,530
  • 1
  • 14
  • 37
  • Consider using `.'` instead of `'` to transpose since without the point it would be `ctranspose` not `transpose`. That creates problems when the values are complex since the complex conjugate transpose would be calculated. – Matt Jul 08 '16 at 05:32
1

To get X1 - mean(X1) you could try

X = [X1(:,1) - mean(X1(:,1)), X1(:,2) - mean(X1(:,2))]

this will output

    1.  - 1.8  
  - 1.    0.2  
  - 1.  - 0.8  
    0.    2.2  
    1.    0.2  
StaticBeagle
  • 5,070
  • 2
  • 23
  • 34