user466534
user466534

Reputation:

make given matrix zero centered

suppose that we have following matrix

A=rand(4,3)

A =

    0.9572    0.4218    0.6557
    0.4854    0.9157    0.0357
    0.8003    0.7922    0.8491
    0.1419    0.9595    0.9340

i want to calculate means of each columns of matrix and then subtract these means from original matrix,so i have calculates means

  b=mean(A)

b =

    0.5962    0.7723    0.6186

as i know to subtract means,we should use something like this

 A-repmat(b,1,3)
Error using  - 
Matrix dimensions must agree.

but it shows me error,so please help me what is problem?

Upvotes: 1

Views: 844

Answers (1)

user466534
user466534

Reputation:

my answer in this case after several recommendation and helping,is following

A=rand(4,3)

A =

    0.7094    0.6551    0.9597
    0.7547    0.1626    0.3404
    0.2760    0.1190    0.5853
    0.6797    0.4984    0.2238

and solutions

A-repmat(B,4,1)

ans =

    0.1044    0.2963    0.4324
    0.1497   -0.1962   -0.1869
   -0.3289   -0.2398    0.0580
    0.0748    0.1396   -0.3035

and another approaches

 bsxfun(@minus,A,mean(A))

    ans =

        0.1044    0.2963    0.4324
        0.1497   -0.1962   -0.1869
       -0.3289   -0.2398    0.0580
        0.0748    0.1396   -0.3035

thanks guys very much

Upvotes: 1

Related Questions