필터 지우기
필터 지우기

How do I use for end statements?

조회 수: 2 (최근 30일)
Noah Kebschull
Noah Kebschull 2017년 12월 12일
댓글: Noah Kebschull 2017년 12월 12일
I've asked this question before, but I'm still not grasping how to use for-end statements. I have a random vector v, and I need to find the number of negative elements, positive elements, the sum of all positive elements, and the product of all elements between a specific range, all within the same vector. I know how to do this without using a for-end statement with no problem, but I have to know how to do this using a for-end statement and I'm having some difficulty.

채택된 답변

Walter Roberson
Walter Roberson 2017년 12월 12일
neg_count = 0;
pos_count = 0;
pos_total = 0;
range_prod = 1;
for idx = 1 : length(v)
if v(idx) is negative
neg_count = neg_count + 1;
end
...
if v(idx) is in the particular range
range_prod = range_prod * v(idx);
end
end
  댓글 수: 1
Noah Kebschull
Noah Kebschull 2017년 12월 12일
That helps a lot, thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by