Count how many elements are present inside arrays
古いコメントを表示
Given the following vector
V= [1 2 3 4 7 8 9 10 11 12 14 16 17];
and the vectors
P = [1 2 3 4 7 8 9 10 11 12 14 16 17 2 7 9 11 12 3 1 10];
F = [1 2 3 4 7 9 10 11 12 14 16 17 3 7 9 11 14 16 1];
I want to create two new vectors A and B that count how many times the element in V are present in P and F.
A= [2 2 2 1 2 1 2 2 2 2 1 1 1 ]. % Refer to P
Element 1 is present 2 times in P. Element 2 is present 2 times in P.... Element 8 is present 1 time in P. Element 9 is present 2 times in P ...
B= [2 1 2 1 2 0 2 1 2 1 2 2 1]. %Refer to F
Element 1 is present 2 times in F. Element 2 is present 1 times in F.... Element 8 is present 0 time in F. Element 9 is present 2 times in F ...
1 件のコメント
This comment is JFF (just for fun) but I thought I'd share the results of a speed comparison between the variety of solutions below (which are all so fast that speed should not be a deciding factor).
From each solution below, the single line that computes the variable "A" was iteratively timed 100,000 times using tic/toc. Bar heights show the median time values and errorbars show the 95% confidence interval using the percentile method.

Of the methods that do not require implicit expansion (and therefore work on releases prior to r2016b), histcounts is the winner (done on r2019a, Windows 7, 64bit, Intel i5 @2.5GHz).
採用された回答
その他の回答 (3 件)
Here is an anonymous function that can be applied to any two vectors. The vectors can be any lengths and any orientation (row or column). This uses implicit expansion which became available in r2016b.
% function: counts the number of times elements of v1 are found in v2
% v1 & v2 are vectors (column or row, any lengths)
% output: a row vector the same lenght as v1
matchCountFcn = @(v1, v2)sum(v1(:)==v2(:).',2).';
A = matchCountFcn(V,P);
B = matchCountFcn(V,F);
3 件のコメント
madhan ravi
2019 年 9 月 12 日
Why not use
sum(V==P.') %?
Adam Danz
2019 年 9 月 12 日
That is what I'm doing but I'm forcing 'V' to be a col vec and 'P' to be a row vec.
The unrestrained version you suggested requires the user to enter the correct shape and the user can never be trusted :D
madhan ravi
2019 年 9 月 12 日
"the user can never be trusted :D"
Yes that's funny and completely a true fact xd.
David Hill
2019 年 9 月 12 日
A=arrayfun(@(x)sum(P==x),V);
B=arrayfun(@(x)sum(F==x),V);
Steven Lord
2019 年 9 月 12 日
V= [1 2 3 4 7 8 9 10 11 12 14 16 17];
P = [1 2 3 4 7 8 9 10 11 12 14 16 17 2 7 9 11 12 3 1 10];
F = [1 2 3 4 7 9 10 11 12 14 16 17 3 7 9 11 14 16 1];
A = histcounts(P, [V Inf])
B = histcounts(F, [V Inf])
I added Inf at the end of the edges stored in V so the last bin counts elements in P or F in the range [17, Inf] (which in this case counts just those elements that are exactly equal to 17) and the next-to-last bin counts edges in [16, 17).
If I hadn't done that the last bin would count elements in [16, 17].
カテゴリ
ヘルプ センター および File Exchange で Data Distribution Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!