9
$\begingroup$

I have matrix and need to subtract another matrix element by element on each row. Something like this:

$$ \begin{pmatrix} x_{1} & x_{2}\\ x_{3} & x_{4}\\ \vdots & \vdots\\ x_{n-1} & x_{n}\\ \end{pmatrix} - \begin{pmatrix} y_{1} & y_{2}\\ \end{pmatrix} $$

So end result should be something like:

$$ \begin{pmatrix} x_{1} - y_{1} & x_{2} - y_{2}\\ x_{3} - y_{1} & x_{4} - y_{2}\\ \vdots & \vdots\\ x_{n-1} - y_{1} & x_{n} - y_{2}\\ \end{pmatrix} $$

How to do this? How to do this in Octave, Matlab?

Sorry for noob question. Also would be very kind if you pint me where to read about this.

$\endgroup$
4
  • $\begingroup$ for loop is always a good start. $\endgroup$
    – picakhu
    Commented Nov 29, 2011 at 22:12
  • $\begingroup$ 'for loop' is done :), now need more. I found solution - bsxfun(@minus, X, Y).. $\endgroup$ Commented Nov 29, 2011 at 22:16
  • $\begingroup$ @moderators, pls, close this topic, this mostly exact duplicate of the math.stackexchange.com/questions/5793/… $\endgroup$ Commented Nov 29, 2011 at 22:18
  • $\begingroup$ How about x - repmat (y, [n 1]) ; $\endgroup$
    – littleO
    Commented May 24, 2016 at 21:23

5 Answers 5

4
$\begingroup$

With the current version (3.6) of Octave, simply subtracting will work

> a = [1 2; 3 4; 5 6; 7 8]
> b = [1 -1]
> a - b
ans =

   0   3
   2   5
   4   7
   6   9

Edit: Apparently this will also work in Matlab starting with the upcoming release (2016b).

$\endgroup$
2
  • $\begingroup$ In Octave (3.4) this gives an error: a - b error: operator -: nonconformant arguments (op1 is 4x2, op2 is 1x2) $\endgroup$
    – asmaier
    Commented Oct 27, 2013 at 19:15
  • $\begingroup$ This depends if the feature called "Automatic Broadcasting" is turned on or not. You can turn it off to make make sure your code is more compatible with Matlab if you want to. $\endgroup$ Commented May 24, 2016 at 21:04
3
$\begingroup$

Solution from Stackoveflow - https://stackoverflow.com/a/1773119/38975

bsxfun(@minus, X, y);
$\endgroup$
1
  • $\begingroup$ This works also in Octave 3.4 $\endgroup$
    – asmaier
    Commented Oct 27, 2013 at 19:23
1
$\begingroup$

If your matrices are only two columns, here's a nasty way to do it:

>> a = [1 2; 3 4; 5 6; 7 8]
>> b = [1 -1]

>> [a(:,1)-b(1),a(:,2)-b(2)]
ans =

   0   3
   2   5
   4   7
   6   9

I suspect there's a better way though ...

$\endgroup$
1
$\begingroup$

The following is also a Kronecker product shortcut and is quite general: Suppose your $X,y_1,y_2$ is in the workspace, then

result = X - kron(ones(size(X,1),1),[y1 y2]);

gives you the ... result :)

$\endgroup$
1
$\begingroup$

Another way to do it is by outer product:

> a = [1 2; 3 4; 5 6; 7 8]
> b = [1 -1]
> a - ones(4,1)*b

This easily gets nastier to express with outer products if you start having 3,4 or even more dimensions and want to replicate any subset of them though.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .