How can I count how many numbers are different from '-1'?
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi,
Example: Suppose we a have a matrix A
A= [1 -1 0 -1 0 0 -1 0 -1 -1 -1 -1 -1 -1 -1]
How can I count how many numbers are different from '-1'?
댓글 수: 2
Shubhashree Bal
2019년 11월 1일
Search for how many negative number exists in A. than subtract that from length of array. Or Write a loop for how many positive number exists in array.
Code attached below:
Close all;
clear all;
A= [1 -1 0 -1 0 0 -1 0 -1 -1 -1 -1 -1 -1 -1];
count = 0;
for i = 1: length(A)
if(A(i)>=0)
count = count +1;
end
end
답변 (2개)
Pedro Martinez
2019년 11월 1일
Total = sum (A==-1);
댓글 수: 1
Walter Roberson
2019년 11월 5일
That gives the number of rows of -1 but the user wants the number of columns that are not -1
참고 항목
카테고리
Help Center 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!