From: Jose on
Hello,

I have a vector, and I need to count how many times a determined value in found inside a vector.

for example if I have the vector: V=[ 2, 5, 4, 2, 0, 9, 8, 0 ]
the value "0" in found twice.
Which function can I use to count how many times a determined values is found?

thank you very much!
From: Andy on
"Jose " <mylordjose(a)hotmail.com> wrote in message <hhapcl$jp7$1(a)fred.mathworks.com>...
> Hello,
>
> I have a vector, and I need to count how many times a determined value in found inside a vector.
>
> for example if I have the vector: V=[ 2, 5, 4, 2, 0, 9, 8, 0 ]
> the value "0" in found twice.
> Which function can I use to count how many times a determined values is found?
>
> thank you very much!

sum(V==0);
From: Vince Petaccio on
"Jose " <mylordjose(a)hotmail.com> wrote in message <hhapcl$jp7$1(a)fred.mathworks.com>...
> Hello,
>
> I have a vector, and I need to count how many times a determined value in found inside a vector.
>
> for example if I have the vector: V=[ 2, 5, 4, 2, 0, 9, 8, 0 ]
> the value "0" in found twice.
> Which function can I use to count how many times a determined values is found?
>
> thank you very much!

You can use the find function to create a list of indices of a particular value in the vector, then determine the "count" based on the length of the index vector.

Ex.

V=V=[ 2, 5, 4, 2, 0, 9, 8, 0 ];

ind = find(V==0)
ans =
5
8

length(ind)
ans =
2

To make this quicker,

num_zeros = length(find(V==0))
num_zeros = 2

-Vince
From: ImageAnalyst on
Here's a third way, using nnz -- a function to find the number of non-
zeros:

% Create some sample data.
V=[ 2, 5, 4, 2, 0, 9, 8, 0 ]
% We don't have to, but let's use a variable for the number to look
for.
valueToLookFor = 0; % Use whatever value you want.
% Count how many times it occurs.
numOccurrences = nnz(V == valueToLookFor)
From: Jose on



Thank you very much for all the anwers!.
It helped me very much!!

Jose