フィルターのクリア

How do I get an index like with ismember, but with values representing the number of occurances?

7 ビュー (過去 30 日間)
I have some vectors, let's call them A and B, and I want to find all elements of A contained within B. So far I've gotten an logical index of all the unique elements, but I need to go one step further and count up repetitions as well. So for instance:
A = ['monday', 'tuesday', 'thursday', 'monday']
B = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
Index = [2 1 0 1 0 0 0]

採用された回答

Stephen23
Stephen23 2017 年 3 月 1 日
編集済み: Stephen23 2017 年 3 月 1 日
>> A = {'monday', 'tuesday', 'thursday', 'monday'};
>> B = {'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'};
>> idx = cellfun(@(s)nnz(strcmp(A,s)),B)
idx =
2 1 0 1 0 0 0

その他の回答 (1 件)

Steven Lord
Steven Lord 2017 年 3 月 1 日
A = {'monday', 'tuesday', 'thursday', 'monday'};
B = {'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'};
Ac = categorical(A, B);
Bc = categorical(B);
histcounts(Ac, Bc)
If your data is numeric you can skip the categorical steps, though you will need to add an extra element to the end of the second input. For categorical data, histcounts uses each category as a bin; for numeric data, it uses the second input as the edges of the bin. By default the leftmost edges are included in the bins. The extra element will be the rightmost edge of the last bin and will catch elements equal to the next to last element of the edges vector.
A = [1 2 4 1];
B = [1:7 Inf];
histcounts(A, B)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by