Counting frequency of occurance of each element of one vector in the another vector

5 ビュー (過去 30 日間)
Suppose I have a vector x=[1 2 3 4 5] and y=[1 1 4 2 1 5 2 5 1 1]. I have to count frequency of occurance of each element of x in y.
For this particular example 1-->5, 2-->2, 3-->0, 4-->1, 5-->2 therefore output should be z=[5 2 0 1 2].
Thank you.

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 3 月 5 日
編集済み: Dyuman Joshi 2023 年 3 月 5 日
x=[1 2 3 4 5];
y=[1 1 4 2 1 5 2 5 1 1];
%hiscounts has an automatic binning algorithm
out1=histcounts(y)
out1 = 1×5
5 2 0 1 2
If you wish to include x as well in the code -
histcounts combines the last two bins into one, so modify the bins accordingly
out2=histcounts(y,[x x(end)+1])
out2 = 1×5
5 2 0 1 2
Using histc(), but note that histc is not recommended to use by MathWorks
out3=histc(y,x)
out3 = 1×5
5 2 0 1 2

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by