Effacer les filtres
Effacer les filtres

Vector subtraction from matrix

3 vues (au cours des 30 derniers jours)
David C
David C le 4 Juin 2016
Let A be a 3 by 5 matrix:
A=rand(3,5);
v=1:5;
How can I add v to each row of A without using a for loop, to improve efficiency?

Réponse acceptée

Star Strider
Star Strider le 4 Juin 2016
If I understand correctly what you want to do, use the bsxfun function:
A=rand(3,5)
v=1:5;
B = bsxfun(@plus, A, v)
A =
0.27247 0.60307 0.30892 0.93688 0.043818
0.77582 0.18401 0.23088 0.031879 0.4249
0.33141 0.087459 0.90919 0.59365 0.52158
B =
1.2725 2.6031 3.3089 4.9369 5.0438
1.7758 2.184 3.2309 4.0319 5.4249
1.3314 2.0875 3.9092 4.5936 5.5216

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by