# Re: In search of elegant indices searching

**URL:** <https://www.ruby-forum.com/t/re-in-search-of-elegant-indices-searching/60667>\
**Category:** Ruby\
**Created:** [May 16, 2006, 5:33pm UTC](https://www.ruby-forum.com/t/re-in-search-of-elegant-indices-searching/60667 "2006-05-16T17:33:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 16, 2006, 5:33pm UTC](https://www.ruby-forum.com/t/re-in-search-of-elegant-indices-searching/60667/1 "2006-05-16T17:33:47Z")

</div>

Dear Tim,

thanks for your reply.  
I am a bit tired today and the task is a little involved,  
so I try to let others work for me 🙂  
I want to evaluate some mathematical function , which has  
arguments ‘x’,‘y’,‘z’ etc. (think of something like

(x,y,z) -\> 5_z_y+x\*y\*\*2)

)  
at different values for these variables x,y,z.  
Actually, I’ve got data, and I am trying to get back that function, or  
an  
approximation to it, from these data, so it’s actually a (multivariate)  
interpolation problem. To do that efficiently, I need to hold  
several variable values fixed at some point in time while varying  
others.

So, in my example, if I have a Hash like

my\_hash={‘x’,[1,1,3,1,5,6],‘w’,[1,2,3,1,4,5],‘y’,[1,1,1,4,4,6]} ,

then these are coordinates where the unknown function is evaluated at.

If, now, the nice and wonderful method I search for, gives me

res={{‘x’=\>1,‘w’=\>1}=\>[0,3],{‘x’=\>3,‘w’=\>3}=\>[2],{‘x’=\>5,‘w’=\>4}=\>[4],{‘x’=\>6,  
‘w’=\>5}=\>[5]},

I know that at the 0-th and 3-th measurement point, ‘x’ and ‘w’ didn’t  
vary,  
so, any  
change in the function values is due to the change of ‘y’ from 1 to 4.

Do you have any elegant ideas for that ?

Thank you,

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 16, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/re-in-search-of-elegant-indices-searching/60667/2 "2006-05-16T18:12:22Z")

</div>

> my\_hash={‘x’,[1,1,3,1,5,6],‘w’,[1,2,3,1,4,5],‘y’,[1,1,1,4,4,6]} ,

> res={{‘x’=\>1,‘w’=\>1}=\>[0,3],{‘x’=\>3,‘w’=\>3}=\>[2],{‘x’=\>5,‘w’=\>4}=\>[4],{‘x’=\>6,  
> ‘w’=\>5}=\>[5]},
> 
> I know that at the 0-th and 3-th measurement point, ‘x’ and ‘w’ didn’t vary,  
> so, any change in the function values is due to the change of ‘y’ from 1 to 4.

I still don’t get it:

{‘x’=\>1,‘w’=\>1}=\>[0,3]  
means the 0th and 3rd point are both ‘1’

{‘x’=\>3,‘w’=\>3}=\>[2]  
means the 2nd point is 3 in both

{‘x’=\>5,‘w’=\>4}=\>[4]  
now it gets weird, this means the 4th position of x is 5 and of w is  
4… Why only that result? Seems a little arbitrary, why doesn’t the  
result contain:  
{‘x’=\>1,‘w’=\>2}=\>[1]  
?

And how come `y` is left out of the results alltogether?  
How about this:  
def whatever arr1, arr2  
h = {}  
arr1.each\_with\_index { |val, i|  
h[[val, arr2[i]]] ||= []  
h[[val, arr2[i]]].push i  
}  
h  
end

whatever([1,1,3,1,5,6], [1,2,3,1,4,5]) # =\> {[1, 2]=\>[1], [5, 4]=\>[4],  
[3, 3]=\>[2], [1, 1]=\>[0, 3], [6, 5]=\>[5]}

-tim
