Count the occurence certain elements based on condition in next column
古いコメントを表示
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!!
採用された回答
その他の回答 (1 件)
Ameer Hamza
2020 年 11 月 3 日
編集済み: Ameer Hamza
2020 年 11 月 3 日
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 件のコメント
Daniar Fahmi
2020 年 11 月 4 日
Ameer Hamza
2020 年 11 月 4 日
I am glad to be of help!!!
Daniar Fahmi
2020 年 11 月 4 日
編集済み: Daniar Fahmi
2020 年 11 月 4 日
Ameer Hamza
2020 年 11 月 4 日
Can you share your data in a .mat file?
Daniar Fahmi
2020 年 11 月 4 日
Ameer Hamza
2020 年 11 月 4 日
Following solution will be more robust
A = dataPD(1,:);
B = dataPD(2,:);
C = [1:max(A); histcounts(A(B~=0), 0.5:1:max(A)+0.5)].'
Daniar Fahmi
2020 年 11 月 4 日
Bruno Luong
2020 年 11 月 4 日
編集済み: Bruno Luong
2020 年 11 月 4 日
Attention A of your data contains 82335 0s and they are discarded from counting
Daniar Fahmi
2020 年 11 月 4 日
カテゴリ
ヘルプ センター および File Exchange で Variables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!