How to add a value to a row vector x number of times in a loop to get an array of iterations
    조회 수: 4 (최근 30일)
  
       이전 댓글 표시
    
Hi,
I am trying to add a value to a row vector a set number of times but keep the first iteration and move it to the next row, I've been trying to use a for loop but I cannot get it to work correctly. Here is an example,
%
 A = [-100 -200 -150 -50]
 B = [10 20 15 5]
I want the resulting array C to look like
%
 C = [-100 -200 -150 -50
      -90  -180 -135 -45
      -80  -160 -120 -40]
I've been trying to use the following code
%
for i = 1:4
  for j = 1:3
      C(i,j) = A(i,j) - B(i,j);
  end
end
If anyone has any advice that'd be great.
댓글 수: 0
채택된 답변
  Azzi Abdelmalek
      
      
 2016년 4월 13일
        A = [-100 -200 -150 -50]
B = [10 20 15 5]
r=bsxfun(@times,B,(0:2)')
out=bsxfun(@plus,r,A)
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 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!

