StatsViaCsh
StatsViaCsh

Reputation: 2640

How do I subtract the values of a 1x2 matrix from a 2 x 2 matrix in octave?

I'm brand new to Octave, and I'm trying to do some basic matrix operations. I'll give a simple example of what I'm trying to do (actual data set is much larger).

a = [1 2; 2 4]
mu = mean(a)
normalized = a - mu %error line

So in my example, the mu values are 1.5 and 6. I'd like to get a matrix back that has 1.5 subtracted from the first column, and 3 subtracted from the 2nd.

Such as:

-.5 -1 .5 1

Big thanks in advance.

Upvotes: 1

Views: 553

Answers (3)

Dan
Dan

Reputation: 45751

In Maltab (and it will work in Octave too) you would do it using a binary singelton expansion:

bsxfun(@minus, a, mu)

However, my understanding is that Octave does broadcasting automatically for you and so you shouldn't get an error trying to subtract a 1x2 matrix from a 2x2... :/

Upvotes: 3

Ben Jackson
Ben Jackson

Reputation: 93710

normalized = a - repmat(mu, size(a,1), 1)

Upvotes: 1

Stewie Griffin
Stewie Griffin

Reputation: 14939

normalized  = [a(:,1) - mu(1), a(:,2) - mu(2)];

Upvotes: 1

Related Questions