how to find out how many times a number apears in a 2D matrix ? which function to use?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
aya qassim
am 5 Jan. 2019
Bearbeitet: madhan ravi
am 7 Jan. 2019
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 Kommentar
Image Analyst
am 7 Jan. 2019
Bearbeitet: Image Analyst
am 7 Jan. 2019
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?
Akzeptierte Antwort
madhan ravi
am 5 Jan. 2019
n=2; % number to be tested
nnz(ismember(x,n))
2 Kommentare
madhan ravi
am 7 Jan. 2019
Bearbeitet: madhan ravi
am 7 Jan. 2019
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)]
Weitere Antworten (1)
Walter Roberson
am 6 Jan. 2019
[ux, ia, ic] = unique(x);
output = [ux, accumarray(ic, 1)]
0 Kommentare
Siehe auch
Kategorien
Mehr zu Graphics Object Properties finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!