how to create a matrix from a loop using other matrix
    조회 수: 3 (최근 30일)
  
       이전 댓글 표시
    
c = [1
    2
    3
    4
    5
    6
    7
    8
    9
    10];
d = [1
    0
    0
    -1
    0
    0
    1
    0
    1
    1];
i = zeros(numel(d),1);
for i1 = 2:numel(i)
    if d(i1) == 1
        i(i1) = c(i1);
    elseif d(i1) == -1
        i(i1) = c(i1)*-1;
    else
        i(i1) = i(i1-1);
    end
end
댓글 수: 2
  dpb
      
      
 2017년 7월 12일
				How to vectorize I presume is the ?
? to OP, though...your loop will only set the value to succeeding for those after the first as it is sequential and passes by the first element. Is this intended or is it intended that all elements in i end up nonzero in the end?
Also, while I don't have an issue with using i as a loop index or temporary in defiance of the builtin definition, I would recommend to NOT use it as a result variable array as here. Pick another variable name for the real case.
답변 (1개)
참고 항목
카테고리
				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!