Vector subtraction from matrix

1 visualización (últimos 30 días)
David C
David C el 4 de Jun. de 2016
Respondida: Star Strider el 4 de Jun. de 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?

Respuesta aceptada

Star Strider
Star Strider el 4 de Jun. de 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

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by