필터 지우기
필터 지우기

how to find out how many times a number apears in a 2D matrix ? which function to use?

조회 수: 2 (최근 30일)
Hey,
how can I find how many time a numbers appears in an 2D matrix for example:
If I have a matrix
x=[1,3,4;2,2,1;2,2,1]
number 2 appears 4 times
numger 1 appears 2 times
number 3 appears 1 time
Desired output:
22 1 3
22 1
  댓글 수: 1
Image Analyst
Image Analyst 2019년 1월 7일
편집: Image Analyst 2019년 1월 7일
You can't have an array with ragged right edge. Arrays must be rectangular. What do you want in the space below the 3? Or else you could have a 1-by-3 cell array, ca where
ca{1} = [2, 2; 2, 2];
ca{2} = [1;1];
ca{3} = 3;
Is that what you want?
Anyway, 1 appears 3 times, not 2 times, and what about 4? Why is 4 not in your desired output?

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

채택된 답변

madhan ravi
madhan ravi 2019년 1월 5일
n=2; % number to be tested
nnz(ismember(x,n))
  댓글 수: 2
aya qassim
aya qassim 2019년 1월 6일
편집: madhan ravi 2019년 1월 7일
thank you,
But if I only have the matrix as an input (I don't have an input fot the number ) , how can I find out how many time a number apprears?
madhan ravi
madhan ravi 2019년 1월 7일
편집: madhan ravi 2019년 1월 7일
Try the below , a in result represent unique numbers and the corresponding histc() shows how many times they occur in the matrix:
a = unique(x);
result = [a histc(x(:),a)]

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2019년 1월 6일
[ux, ia, ic] = unique(x);
output = [ux, accumarray(ic, 1)]

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by