From: Judy on 3 Mar 2010 14:44 What is the fastest way to search for a value in an array to see if there is a match? For example..... I am looking for any 5's and want to see if there is a quick way to check if there is a 5 in a large array -- i.e. a=zeros(100,100); a(23,64)=5; I am sure I can use for loops.. but just wanted to see if there is any builtin functions already. Mahalo!
From: Sean on 3 Mar 2010 14:55 "Judy " <sauwen.jl(a)gmail.com> wrote in message <hmme64$eol$1(a)fred.mathworks.com>... > What is the fastest way to search for a value in an array to see if there is a match? For example..... > I am looking for any 5's and want to see if there is a quick way to check if there is a 5 in a large array -- i.e. a=zeros(100,100); a(23,64)=5; > > I am sure I can use for loops.. but just wanted to see if there is any builtin functions already. > > Mahalo! >>help any
From: Walter Roberson on 3 Mar 2010 14:45 Judy wrote: > What is the fastest way to search for a value in an array to see if > there is a match? For example..... > I am looking for any 5's and want to see if there is a quick way to > check if there is a 5 in a large array -- i.e. a=zeros(100,100); > a(23,64)=5; > > I am sure I can use for loops.. but just wanted to see if there is any > builtin functions already. ismember() will make it much more -convenient-. The _fastest_ way is going to depend upon lots of details such as processor type, primary cache size, the exact size and placement of the array and how much cache-line aliasing there is for it, the Matlab version you are using, the data type of the array, and many other details. In the example you posted, the fastest way might be to use sparse arrays.
From: Matt Fig on 3 Mar 2010 14:58 any(a(:)==5) Also, beware floating point issues!
From: Joseph Kirk on 3 Mar 2010 15:03
"Judy " <sauwen.jl(a)gmail.com> wrote in message <hmme64$eol$1(a)fred.mathworks.com>... > What is the fastest way to search for a value in an array to see if there is a match? For example..... > I am looking for any 5's and want to see if there is a quick way to check if there is a 5 in a large array -- i.e. a=zeros(100,100); a(23,64)=5; > > I am sure I can use for loops.. but just wanted to see if there is any builtin functions already. > > Mahalo! If you just want to know if there are any 5's but don't care where they are or how many, use: any5 = any(a(:) == 5) If you want the indices of every 5 in the matrix, use: index = find(a == 5) ~or~ [rowIndex,colIndex] = find(a == 5) |