Subtracting Vector from a Matrix
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (1)
Adam Danz
il 21 Nov 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 Commenti
Paul Rogers
il 21 Nov 2019
Jan
il 21 Nov 2019
Obviously the sizes are not matching. Trust Matlab. See:
size(a)
size(b)
What do you get?
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.
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


