Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to find elements of a given value in a matrix?

Subject: How to find elements of a given value in a matrix?

From: juckou

Date: 25 Dec, 2008 18:56:52

Message: 1 of 10

Dear all,

I'm just trying to know how many elements in a matrix have a given value. I mean, if I have a matrix A=[1 2 3; 1 2 3; 1 2 3] I want to know the number (or the percentage) of elements that are "1" (3 or the 33,3% in this case).

Any help?
Thank you very much

julio

Subject: How to find elements of a given value in a matrix?

From: Nasser Abbasi

Date: 25 Dec, 2008 19:27:28

Message: 2 of 10


"juckou" <ja79@hw.ac.uk> wrote in message
news:16525645.1230231468100.JavaMail.jakarta@nitrogen.mathforum.org...
> Dear all,
>
> I'm just trying to know how many elements in a matrix have a given value.
> I mean, if I have a matrix A=[1 2 3; 1 2 3; 1 2 3] I want to know the
> number (or the percentage) of elements that are "1" (3 or the 33,3% in
> this case).
>
> Any help?
> Thank you very much
>
> julio

 numel(find(A==1))/numel(A)

ans =

    0.3333

--Nasser

Subject: How to find elements of a given value in a matrix?

From: Donn Shull

Date: 25 Dec, 2008 19:44:02

Message: 3 of 10

juckou <ja79@hw.ac.uk> wrote in message <16525645.1230231468100.JavaMail.jakarta@nitrogen.mathforum.org>...
> Dear all,
>
> I'm just trying to know how many elements in a matrix have a given value. I mean, if I have a matrix A=[1 2 3; 1 2 3; 1 2 3] I want to know the number (or the percentage) of elements that are "1" (3 or the 33,3% in this case).
>
> Any help?
> Thank you very much
>
> julio

Hello Julio,

You will want to use find and numel ie

>> percent = 100*(numel(find(A==2))/numel(A))

percent =

   33.3333

Good Luck,

Donn

Subject: How to find elements of a given value in a matrix?

From: juckou

Date: 25 Dec, 2008 19:57:00

Message: 4 of 10

Thank you very much, Nasser

julio

Subject: How to find elements of a given value in a matrix?

From: Walter Roberson

Date: 30 Dec, 2008 01:27:32

Message: 5 of 10

juckou wrote:

> I'm just trying to know how many elements in a matrix have a given value. I mean, if I have a
> matrix A=[1 2 3; 1 2 3; 1 2 3] I want to know the number (or the percentage) of elements that
> are "1" (3 or the 33,3% in this case).

Others have recommended find. An alternative is:
sum(A(:)==1) / numels(A)
which should be faster than find()

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: How to find elements of a given value in a matrix?

From: ImageAnalyst

Date: 30 Dec, 2008 05:15:45

Message: 6 of 10

On Dec 25, 1:56=A0pm, juckou <j...@hw.ac.uk> wrote:
> Dear all,
>
> I'm just trying to know how many elements in a matrix have a given value.=
 I mean, if I have a matrix A=3D[1 2 3; 1 2 3; 1 2 3] I want to know the nu=
mber (or the percentage) of elements that are "1" (3 or the 33,3% in this c=
ase).
>
> Any help?
> Thank you very much
>
> julio

------------------------------------
julio:
If you want to know for ALL numbers (e.g. 1, 2, and 3), then you can
use the hist() function. If you want to know for only one certain
specific number, then you can use the methods the others have given
you.
Regards,
ImageAnalyst

Subject: How to find elements of a given value in a matrix?

From: us

Date: 30 Dec, 2008 08:46:01

Message: 7 of 10

juckou
> I'm just trying to know how many elements in a matrix have a given value...

one of the many solutions

% the data
     m=[
          1 -2 pi
          -2 1 -1
          1 1 pi
     ];
% the engine
     [mu,mx,mx]=unique(m(:));
     r=accumarray(mx,m(:),[],@numel);
     rp=100*r./numel(m);
% the result
     disp([mu,r,rp]);
%{
          -2 2 22.222
          -1 1 11.111
           1 4 44.444
           3.1416 2 22.222
%}

us

Subject: How to find elements of a given value in a matrix?

From: Yuri Geshelin

Date: 30 Dec, 2008 14:23:02

Message: 8 of 10

Walter Roberson <roberson@hushmail.com> wrote in message <bOe6l.26641$wV2.14471@newsfe07.iad>...

> An alternative is:
> sum(A(:)==1) / numels(A)
> which should be faster than find()

I decided to verify this statement by means of this fragment:

clear
A=[1 2 3; 1 2 3; 1 2 3];

tic
for i = 1:1e5
    sum(A(:)==1) / numel(A);
end
Walter_time = toc;

tic
for i = 1:1e5
    numel(find(A==1))/numel(A);
end
Nasser_time = toc;

Walter_time > Nasser_time

ans =

     1

I was about to write: No Walter, your method takes more time... But then I just swapped the two loops:

clear
A=[1 2 3; 1 2 3; 1 2 3];

tic
for i = 1:1e5
    numel(find(A==1))/numel(A);
end
Nasser_time = toc;

tic
for i = 1:1e5
    sum(A(:)==1) / numel(A);
end
Walter_time = toc;

Walter_time > Nasser_time

ans =

     0
-------------
OK, I thought, I will do it another way:

clear
A=[1 2 3; 1 2 3; 1 2 3];
for i = 1:1000
    tic, sum(A(:)==1) / numel(A);
    Walter_time = toc;
    tic, numel(find(A==1))/numel(A);
    Nasser_time = toc;
    d(i) = Walter_time > Nasser_time;
end
sum(d==1)/1000

ans =

    0.9690

This means that in 96.7% of single passes of the loop Walter's method is slower. But then again, I swapped the lines:

>> clear
A=[1 2 3; 1 2 3; 1 2 3];
for i = 1:1000
    tic, numel(find(A==1))/numel(A);
    Nasser_time = toc;
    tic, sum(A(:)==1) / numel(A);
    Walter_time = toc;
    d(i) = Walter_time > Nasser_time;
end
sum(d==1)/1000

ans =

    0.0120

The result is reverse. It turns out that whichever fragment is executed first, it takes more time. Why is that? How can you really know which method is faster?

Subject: How to find elements of a given value in a matrix?

From: someone

Date: 30 Dec, 2008 14:54:01

Message: 9 of 10

"Yuri Geshelin" <geshelin@hotmail.com> wrote in message <gjdas6$56h$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <bOe6l.26641$wV2.14471@newsfe07.iad>...
>
> > An alternative is:
> > sum(A(:)==1) / numels(A)
> > which should be faster than find()
>
> I decided to verify this statement by means of this fragment:
>
> clear
> A=[1 2 3; 1 2 3; 1 2 3];
>
> tic
> for i = 1:1e5
> sum(A(:)==1) / numel(A);
> end
> Walter_time = toc;
>
> tic
> for i = 1:1e5
> numel(find(A==1))/numel(A);
> end
> Nasser_time = toc;
>
> Walter_time > Nasser_time
>
> ans =
>
> 1
>
> I was about to write: No Walter, your method takes more time... But then I just swapped the two loops:
>
> clear
> A=[1 2 3; 1 2 3; 1 2 3];
>
> tic
> for i = 1:1e5
> numel(find(A==1))/numel(A);
> end
> Nasser_time = toc;
>
> tic
> for i = 1:1e5
> sum(A(:)==1) / numel(A);
> end
> Walter_time = toc;
>
> Walter_time > Nasser_time
>
> ans =
>
> 0
> -------------
> OK, I thought, I will do it another way:
>
> clear
> A=[1 2 3; 1 2 3; 1 2 3];
> for i = 1:1000
> tic, sum(A(:)==1) / numel(A);
> Walter_time = toc;
> tic, numel(find(A==1))/numel(A);
> Nasser_time = toc;
> d(i) = Walter_time > Nasser_time;
> end
> sum(d==1)/1000
>
> ans =
>
> 0.9690
>
> This means that in 96.7% of single passes of the loop Walter's method is slower. But then again, I swapped the lines:
>
> >> clear
> A=[1 2 3; 1 2 3; 1 2 3];
> for i = 1:1000
> tic, numel(find(A==1))/numel(A);
> Nasser_time = toc;
> tic, sum(A(:)==1) / numel(A);
> Walter_time = toc;
> d(i) = Walter_time > Nasser_time;
> end
> sum(d==1)/1000
>
> ans =
>
> 0.0120
>
> The result is reverse. It turns out that whichever fragment is executed first, it takes more time. Why is that? How can you really know which method is faster?
>

In my opinion, to begin to see "which method is faster", A needs to be much larger. With only a 3X3 matrix the times are probably due more to "start-up" than anything else (as you have shown). But thats just my opinion, I could be wrong.

Try inserting the statement:

A=[1 2 3; 1 2 3; 1 2 3];
n = 1000; % or larger
A = repmat(A,n,n);

and see what the times are.

Subject: How to find elements of a given value in a matrix?

From: Steven Lord

Date: 2 Jan, 2009 19:43:41

Message: 10 of 10


"Walter Roberson" <roberson@hushmail.com> wrote in message
news:bOe6l.26641$wV2.14471@newsfe07.iad...
> juckou wrote:
>
>> I'm just trying to know how many elements in a matrix have a given value.
>> I mean, if I have a
>> matrix A=[1 2 3; 1 2 3; 1 2 3] I want to know the number (or the
>> percentage) of elements that
>> are "1" (3 or the 33,3% in this case).
>
> Others have recommended find. An alternative is:
> sum(A(:)==1) / numels(A)
> which should be faster than find()

You might also want to compare that with:

nnz(A(:)==1)/numel(A)

I don't know if it'll be faster, but it might.

--
Steve Lord
slord@mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us