[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

*Subject*: Re: findng array[3] in array[3,n]*From*: Craig Markwardt <craigmnet(at)cow.physics.wisc.edu>*Date*: 25 Jan 2001 11:30:05 -0600*Newsgroups*: comp.lang.idl-pvwave*Organization*: U. Wisc. Madison Physics -- Compact Objects*References*: <94pl74$rec$1@ra.nrl.navy.mil>*Reply-To*: craigmnet(at)cow.physics.wisc.edu*Xref*: news.doit.wisc.edu comp.lang.idl-pvwave:23170

"tbowers" <tbowers@nrlssc.navy.mil> writes: > If I have > > a=[ $ > [0,1,2], $ > [3,4,5], $ > [6,7,8]] > > b=[3,4,5] > > how do I find where in a is the row vector b? The answer should > be 1, the 2nd row of a. I've tried many > permutations of where(), but I just don't get it. The only way I > can get an answer is to loop through the rows till i find a match. > What I'm really tryin' to do is to find a color in a color table, > e.g. load RAINBOW color table, (loadct,13) then identify > where a color is. For the color that's listed 6th in the color table > (an almost black) that'd be the 5th row index. Like: ... > Your problem is that WHERE only does a 1D search. So you need to somehow convert your triplets to a single number. Solution 1: 1. Convert your 3 BYTE values to a single LONG value: colorwords = long(r) + ishft(long(g),8L) + ishft(long(b),16L) do the same for your target, and use WHERE to find the match This will be fast if you need a few matches, slow if you need a ton of matches. 2. Search on R value alone using WHERE, then use a FOR loop to scan the resulting matches. wh = where(r EQ rtarg, ct) if ct GT 0 then for i = 0, ct-1 do if ... Probably overkill. 3. Use a Euclidean distance to find the color table entry with the smallest distance from you target value [rtarg, gtarg, btarg]. You should convert R G and B to vectors of type LONG to prevent overflow: dist = (r-rtarg)^2 + (g-gtarg)^2 + (b-btarg)^2 wh = where(dist EQ min(dist)) This will be the most robust to small variations in the color table (ie, if an exact match doesn't exist). Any other ideas? Craig -- -------------------------------------------------------------------------- Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response --------------------------------------------------------------------------

**Follow-Ups**:**Re: findng array[3] in array[3,n]***From:*David Fanning

**Re: findng array[3] in array[3,n]***From:*tbowers

**References**:**findng array[3] in array[3,n]***From:*tbowers

- Prev by Date:
**Re: findng array[3] in array[3,n]** - Next by Date:
**Re: findng array[3] in array[3,n]** - Prev by thread:
**Re: findng array[3] in array[3,n]** - Next by thread:
**Re: findng array[3] in array[3,n]** - Index(es):