필터 지우기
필터 지우기

How to check if there are more ones than zeros in the variable.

조회 수: 3 (최근 30일)
Luccas S.
Luccas S. 2022년 7월 22일
답변: Image Analyst 2022년 7월 22일
For example, if there is the variable x;
x = [1 1 1 1 1 1 1 1 0 0 0];
How to check if it has ones than zeros?
  댓글 수: 1
Mohammad Sami
Mohammad Sami 2022년 7월 22일
if the verctor only contains 1s and 0s, you can just do a sum divided by length of the vector

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

채택된 답변

Walter Roberson
Walter Roberson 2022년 7월 22일
x = [1 1 1 1 1 1 1 1 0 0 0]
x = 1×11
1 1 1 1 1 1 1 1 0 0 0
has_more_ones = mean(x) > 0.5 %note that exactly equal does not qualify as "more"
has_more_ones = logical
1

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 7월 22일
Another way (assuming only 1s and 0s in the array):
moreOnes = nnz(x) > numel(x)/2

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by