Suming vectors which are produced in a loop
이전 댓글 표시
Say I have a loop which returns three vectors:
for i = 1:3
V = [i; 2*i; 3*i];
end
returning
V = [1; 2; 3] V = [2; 4; 6] V = [3; 6; 9]
how do i then sum these to get
sumV = [1+2+3; 2+4+6; 3+6+9]
댓글 수: 1
Stephen23
2018년 3월 27일
Why not just
>> 6:6:18 ans = 6 12 18
Or
>> 6*(1:3) ans = 6 12 18
Or using bsxfun:
>> sum(bsxfun(@times,1:3,(1:3).'),1) ans = 6 12 18
Or if you really want to use a loop (which is a waste of MATLAB):
>> V = zeros(1,3); >> for k = 1:numel(V), V(k)=sum(k*(1:3)); end >> V V = 6 12 18
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!