How to sum values of an array of variables, not the sum of the true/false values (0,1)?
    조회 수: 4 (최근 30일)
  
       이전 댓글 표시
    
I'n very new to matlab! The code itself does work but and i've had to simplify it because it's for coursework, but i have a for loop with if and and else if inside it to meet some conditions
s=0;
for i=1:N;
  if 22==X(i) & 0<Y(i) & 2002==Z(i);
      s=s+1;
      else if  22==C(i) & 0<D(i) & 2002==Z(i);
      s=s+1;  
      end
  end
end
but the problem is that I think it changes the values to 0/1 according to whether it's true or not, but i want the actual sum of values of the goals (here it's Y and D)?
댓글 수: 0
채택된 답변
  Star Strider
      
      
 2015년 10월 14일
        I’m not certain what you’re asking, and it’s difficult to follow your code, but if you want to add ‘Y(i)’ and ‘D(i)’ to ‘s’, do it explicitly:
s = s + Y(i);
or perhaps:
Ysum = Ysum + Y(i);
&c.
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
			
	제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!