Count how many elements are present inside arrays

3 visualizzazioni (ultimi 30 giorni)
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 Commento
Adam Danz
Adam Danz il 12 Set 2019
Modificato: Adam Danz il 12 Set 2019
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).

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 12 Set 2019
>> A = hist(P,V)
A =
2 2 2 1 2 1 2 2 2 2 1 1 1
>> B = hist(F,V)
B =
2 1 2 1 2 0 2 1 2 1 2 2 1
  1 Commento
Adam Danz
Adam Danz il 12 Set 2019
Modificato: Adam Danz il 12 Set 2019
Nice. Unlike my answer, this does not require implicit expansion and would work on releases prior to r2016b. Due to overhead in hist(), this method is slower than the anonymous function version but both are extremely fast so speed isn't really a factor here.

Accedi per commentare.

Più risposte (3)

Adam Danz
Adam Danz il 12 Set 2019
Modificato: Adam Danz il 12 Set 2019
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 Commenti
Adam Danz
Adam Danz il 12 Set 2019
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
madhan ravi il 12 Set 2019
"the user can never be trusted :D"
Yes that's funny and completely a true fact xd.

Accedi per commentare.


David Hill
David Hill il 12 Set 2019
A=arrayfun(@(x)sum(P==x),V);
B=arrayfun(@(x)sum(F==x),V);

Steven Lord
Steven Lord il 12 Set 2019
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].

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by