Count the occurence certain elements based on condition in next column
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Daniar Fahmi
am 3 Nov. 2020
Kommentiert: Daniar Fahmi
am 4 Nov. 2020
Hello.
I have a matrix consists of A = [ 1 3 2 2 4 1 2 3 4 1 3] and B = [0 0 -3 0 2 1 4 0 0 -2 0]
How can I determine the occurence of element in A when corresponding value in B is not 0:
C = [
1 2
2 2
3 0
4 1]
Please help me..
Really appreciate it!!
0 Kommentare
Akzeptierte Antwort
Bruno Luong
am 4 Nov. 2020
U=unique(A(:));
[~,J]=ismember(A(B~=0),U);
n=accumarray(J(:),1,size(U));
C=[U,n]
Weitere Antworten (1)
Ameer Hamza
am 3 Nov. 2020
Bearbeitet: Ameer Hamza
am 3 Nov. 2020
Try this
A = [1 3 2 2 4 1 2 3 4 1 3];
B = [0 0 -3 0 2 1 4 0 0 -2 0];
C = [1:max(A); histcounts(A(B~=0), 'BinMethod', 'integers')].'
9 Kommentare
Bruno Luong
am 4 Nov. 2020
Bearbeitet: Bruno Luong
am 4 Nov. 2020
Attention A of your data contains 82335 0s and they are discarded from counting
Siehe auch
Kategorien
Mehr zu Annotations 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!