help-octave
[Top][All Lists]

## RE: Avoiding looping when applying a 'find' function to an array

 From: Hall, Benjamin Subject: RE: Avoiding looping when applying a 'find' function to an array Date: Wed, 13 Apr 2005 12:01:18 -0400

```I think the following should do what you want (although it may not be the
easiest to understand) -- and you'll need the latest version of the
intersect function from octave-forge

Let "a" be the original list of items and "vals" be the quantity associated
with each element of "a".  If "b" contains the same list of items as "a",
but in a different order, then "v2" will be the list of "vals" reordered
like "b" if you do the following (I tried to make the example easy to follow
by giving odd-numbered items a value of 100 and evens a value of 50):

a    = [7 2 4 1 3 6 5];
vals = [100 50 50 100 100 50 100];
#vals = 10*a;   #might be even easier to check...

b    = [1 3 5 7 2 4 6];

[c,ia,ib] = intersect( a, b );
[d,idx]   = sort(ib);

v2 = vals(ia(idx));

[a; vals]
ans =

7    2    4    1    3    6    5
100   50   50  100  100   50  100

[b; v2]
1    3    5    7    2    4    6
100  100  100  100   50   50   50

****************
If you get:

error: element number 2 undefined in return list
error: evaluating assignment expression near line 3, column 11

then you don't have the most recent version of the intersect function which
returns the two new outputs "ia" and "ib", you can get it from
http://octave.sourceforge.net/index/index.html

Hope that helps

-----Original Message-----
Sent: Wednesday, April 13, 2005 10:49 AM
Subject: Avoiding looping when applying a 'find' function to an array

Hi,
I have a problem which can be easily solved by looping, but which I
would like to try to solve by avoiding loops.

I need to transform one array to another equally sized array using a
look-up table.

If the input array is A, then I take the value of each element of array
A one by one, and using the 'find' function, I find the appropriate
value for the matching element for the output array via the look-up
table. Looping over every element in array A works just fine - for each
element in A I can obtain an output value for the output array. However
I can not think of way of doing this same task without looping - that
is, applying the 'find' function to each element and producing an output
array without looping.

If I input:

Location_of_value_for_output_array = find(input_array(1,1) ==
look_up_table)

then I get the correct location in the look-up table for the value
required in the output array, and then I increment the index of
input_array etc - this is slow because the array is 512 times 512 array
(a graphics file) and the look-up table is 574 rows long.

However if I try the following:

Location_of_value_for_output_array = find(input_array(:,:) ==
look_up_table)

Or

Location_of_value_for_output_array = find(input_array == look_up_table)

then I have no success.
Any help would be much appreciated,
Thanks,
Jeremy Harbinson

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```