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

2 次查看(过去 30 天)
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?

采纳的回答

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 中查找有关 Numerical Integration and Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by