Octave operator -: automatic broadcasting operation applied

Vahid Mirjalili picture Vahid Mirjalili · Jun 13, 2013 · Viewed 13.1k times · Source

In octave 3.6.2, I have a matrix X=[1 2 3; 2 4 5; 2 6 5; 2 3 7; 3 6 8; 2 4 6; 3 6 8; 4 7 10] and I want to calculate X-mean(X), which gives me:

octave:2> X-mean(X)
warning: operator -: automatic broadcasting operation applied
ans =

  -1.37500  -2.75000  -3.50000
  -0.37500  -0.75000  -1.50000
  -0.37500   1.25000  -1.50000
  -0.37500  -1.75000   0.50000
   0.62500   1.25000   1.50000
  -0.37500  -0.75000  -0.50000
   0.62500   1.25000   1.50000
   1.62500   2.25000   3.50000

however, when I try the same command on a different machine, it complains that the sizes of matrices do not match:

error: operator -: nonconformant arguments (op1 is 7x3, op2 is 1x3)

Any idea how to activate that "automatic broadcasting operation" which is applied in the first case? (octave versions are the same!)

Answer

emu picture emu · Jun 18, 2013

You can explicitly request broadcasting by calling bsxfun(operation, A, B), so in your case:

bsxfun(@minus, X, mean(X))

Octave reference, Matlab reference