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

1 ビュー (過去 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

カテゴリ

Find more on Migrate GUIDE Apps in Help Center and File Exchange

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by