Asked by luca
on 12 Sep 2019

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 ...

Answer by Stephen Cobeldick
on 12 Sep 2019

Accepted Answer

>> 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

Adam Danz
on 12 Sep 2019

Sign in to comment.

Answer by Adam Danz
on 12 Sep 2019

Edited by Adam Danz
on 12 Sep 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);

madhan ravi
on 12 Sep 2019

Why not use

sum(V==P.') %?

Adam Danz
on 12 Sep 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
on 12 Sep 2019

"the user can never be trusted :D"

Yes that's funny and completely a true fact xd.

Sign in to comment.

Answer by David Hill
on 12 Sep 2019

A=arrayfun(@(x)sum(P==x),V);

B=arrayfun(@(x)sum(F==x),V);

Sign in to comment.

Answer by Steven Lord
on 12 Sep 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].

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Adam Danz (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480085-count-how-many-elements-are-present-inside-arrays#comment_745239

Sign in to comment.