Vector subtraction from matrix

3 次查看(过去 30 天)
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?

采纳的回答

Star Strider
Star Strider 2016-6-4
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by