How to find the index of the values which are bigger than zero the following case?
조회 수: 3 (최근 30일)
이전 댓글 표시
How to find the index of the values which are bigger than zero the following case?
There is a vector M contines three value for example, M = [ 0 0.5 0.7] , Note: there is no negative values
How can I write the following progrom:
the first one if the value in index one bigger than zero ... Then I put a condition
the second one if the value in index two bigger than zero ... Then I put a condition
the third one if the value in index three bigger than zero ... Then I put a condition
댓글 수: 0
채택된 답변
Image Analyst
2022년 8월 31일
Simple:
M = [0, 0.5, 0.7];
% the first one if the value in index one bigger than zero ... Then I put a condition
if M(1) > 0
fprintf('Do something because first element is greater than 0.\n')
end
% the second one if the value in index two bigger than zero ... Then I put a condition
if M(2) > 0
fprintf('Do something because second element is greater than 0\n')
end
% the third one if the value in index three bigger than zero ... Then I put a condition
if M(3) > 0
fprintf('Do something because third element is greater than 0\n')
end
댓글 수: 0
추가 답변 (1개)
Torsten
2022년 8월 31일
편집: Torsten
2022년 8월 31일
M = [ 0 0.5 0.7];
[~,indices] = find(M>0)
댓글 수: 1
Walter Roberson
2022년 8월 31일
Then go ahead and write that series of if/else statements. You will have 8 branches.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!