Effacer les filtres
Effacer les filtres

A 'Matrix Minus' Question!

2 vues (au cours des 30 derniers jours)
Xiao Tang
Xiao Tang le 24 Juin 2012
There are two matrix.
A = [1 4 3 4 5
6 6 8 9 1
1 2 1 4 5
8 1 2 1 5
9 1 9 2 2
8 8 1 6 4];
B = [1 2 9 2 5];
How can I minus each row of Matrix A by B to get C? The result C =
[0 2 -6 2 0
5 4 -1 7 -4
0 0 -8 2 0
7 -1 -7 -1 0
8 -1 0 0 -3
7 6 -8 4 -1]
I know I can do a loop. But is there an easier way? Any suggestion would be appreciated!
The loop is:
m = size(A);
for i = 1 :length(A)
for n = 1:m(1)
C(n,i) = A(n,i) - B(i);
end
end

Réponse acceptée

the cyclist
the cyclist le 24 Juin 2012
bsxfun(@minus,A,B)
  1 commentaire
Xiao Tang
Xiao Tang le 24 Juin 2012
Thanks man. That's exactly what I am looking for.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by