Vector subtraction from matrix

1 visualizzazione (ultimi 30 giorni)
David C
David C il 4 Giu 2016
Risposto: Star Strider il 4 Giu 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?

Risposta accettata

Star Strider
Star Strider il 4 Giu 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

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by