Vector subtraction from matrix
조회 수: 2 (최근 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?
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!