MATLAB Answers

Determine percent of array elements which meet condition

조회 수: 5(최근 30일)
Gabriel Stanley
Gabriel Stanley 2021년 7월 20일
댓글: Star Strider 2021년 7월 20일
How can I check if a certain percentage of the values in an array meet a condition, e.g. are X% of the values of a given logical array=1?

채택된 답변

Star Strider
Star Strider 2021년 7월 20일
A logical array has two states, true=1 or false=0, so al that is necessary is to find the numbers of 1 values with respect to the total number of elements.
TF = randi([0 1], 25) == 1; % Create Array
T = nnz(TF) % Number Of 'true' Values
T = 341
T_Pct = 100*T/numel(TF) % Percent 'true' Values
T_Pct = 54.5600
.
  댓글 수: 2
Star Strider
Star Strider 2021년 7월 20일
As always, my pleasure!
.

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

추가 답변(0개)

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by