How to count number of specific value?

조회 수: 4 (최근 30일)
Justyna Slawska
Justyna Slawska 2016년 12월 15일
댓글: Justyna Slawska 2016년 12월 19일
I have a matrix and I'm trying to count the number of a specific value in rows (i.e. < 4 )
for example 5 2 3 8 7 1 2 2 3
nnz( A(1,:) < 4); --> 6
but I want sth like this 2 and 4

채택된 답변

Image Analyst
Image Analyst 2016년 12월 15일
If you have the Image Processing Toolbox, use bwlabel and regionprops:
A = [5 2 3 8 7 1 2 2 3]
[labeledA, numRegions] = bwlabel(A < 4)
% Get the size of the regions.
props = regionprops(labeledA, 'Area')
elementCounts = [props.Area]
Run the demo and you'll see this in the command window:
A =
5 2 3 8 7 1 2 2 3
labeledA =
0 1 1 0 0 2 2 2 2
numRegions =
2
props =
2×1 struct array with fields:
Area
elementCounts =
2 4
  댓글 수: 1
Justyna Slawska
Justyna Slawska 2016년 12월 19일
And tell me how to get this specific value (2 3 and 1 2 2 3)

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

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2016년 12월 15일
Justyna - if
data = [5 2 3 8 7 1 2 2 3]
then you can use length and https://www.mathworks.com/help/matlab/ref/find.html to determine the number of elements less than four
length(find(data<4))
  댓글 수: 1
Justyna Slawska
Justyna Slawska 2016년 12월 15일
in this way I get the result 6, but I want 2 and 4

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by