Subtracting Vector from a Matrix

I have the matrix a (8x1001) and b (1x1001) as in attached.
a and b can change in size.
How do I substract b from each row of a and put in a new matrix c?
Thanks.

 Respuesta aceptada

Jan
Jan el 21 de Nov. de 2019
Matlab can subtract vectors from matrices automatically since R2016b - so called "auto expanding". Do you use an older version? Then:
c = bsxfun(@minus, a, b)

5 comentarios

Paul Rogers
Paul Rogers el 21 de Nov. de 2019
exactly, I have a 2014b veersion.
thankks a lot
Adam Danz
Adam Danz el 21 de Nov. de 2019
I filled in your Matlab release for this question (see the right panel of this page, copied below). In the future when you're writing your question, don't ignore those fields becuase they are super helpful to us and can save everyone a lot of time.
191121 102655-Subtracting Vector from a Matrix - MATLAB Answers - MATLAB Central.png
Adam Danz
Adam Danz el 21 de Nov. de 2019
Editada: Adam Danz el 21 de Nov. de 2019
In case anyone's interested, I was curious about the efficiency of bsxfun() so I timed the following 2 lines individually 100,000 times using tic/toc and it turns out that the 2nd line (repmat) is consistently 5-6 times faster (though both are extremely fast).
a and b are the data from the OP's mat files.
c = bsxfun(@minus, a, b);
c = a - repmat(b,size(a,1),1); % 5x faster
Jan
Jan el 21 de Nov. de 2019
@Adam: While repmat is (or at least was) an M-function, you can omit the overhead:
c = a - b(ones(1, 8), :);
Adam Danz
Adam Danz el 21 de Nov. de 2019
Editada: Adam Danz el 21 de Nov. de 2019
Good idea, Jan. I would have expected the ones() method to be faster than repmat(), too. But I timed both methods 500000 times and it turns out that the repmat() is actually faster. I repeated this a few times with consistent results. The boxplots below show the results of the 500k reps with outliers removed.
a = repmat((1:8)',1,12);
b = repmat(2,1,12);
% Method 1 (red)
c = a - b(ones(1, 8), :);
% Method 2 (blue)
d = a - repmat(b,size(a,1),1);
And including the outliers
191121 130508-compareSpeedGUI_results.png
Who knows what's going on in either function (closed source).

Iniciar sesión para comentar.

Más respuestas (1)

Adam Danz
Adam Danz el 21 de Nov. de 2019
"How do I substract b from each row of a and put in a new matrix c?"
As long as the number of columns in b matches the number of columns in a
c = a - b;
Demo:
% Create data
a = repmat((1:8)',1,12);
b = repmat(2,1,12);
c = a - b;

3 comentarios

I can't understand, a and b are as in attached, but when I type:
c = a - b;
Error using -
Matrix dimensions must agree.
it keeps saying this. they have the same columns.
Jan
Jan el 21 de Nov. de 2019
Obviously the sizes are not matching. Trust Matlab. See:
size(a)
size(b)
What do you get?
Adam Danz
Adam Danz el 21 de Nov. de 2019
Editada: Adam Danz el 21 de Nov. de 2019
See the 2nd line of my answer:
As long as the number of columns in b matches the number of columns in a
This requirement is met for the data you provided in matrix a and matrix b in your mat files.
load('a.mat')
load('b.mat')
size(a) % 8 1001
size(b) % 1 1001
c = a-b;
size(c) % 8 1001
If that requirement is not met, you cannot perform matrix subtraction without defining how you plan to resolve the size mismatch.
[update]
If your matlab release is prior to r2016a, see Jan's answer which avoids implicit expansion. This is why it's always good to include your matlab release in the field where it is requested while writing your question.

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2014b

Preguntada:

el 21 de Nov. de 2019

Editada:

el 21 de Nov. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by