Welcome Guest! To enable all features please Login. New Registrations are disabled.

Notification

Icon
Error

Login


Options
Go to last post Go to first unread
Offline zorrykid  
#1 Posted : 20 February 2014 13:26:49(UTC)
zorrykid


Rank: Member

Groups: Registered
Joined: 17/02/2014(UTC)
Posts: 18
Italy
Location: Rome

Was thanked: 5 time(s) in 4 post(s)
Was wondering if exists already a function to find where is positioned a number in a sorted array ( or sequence of numbers ) , something similar to the Excel VLOOKUP or is something that we need to develop by ourself with a small program ( just to avoid to reinvent the wheel).
For finding I mean where is the most closer match and its position ..like in Excel.

Thanks in advance

Franco

Wanna join the discussion?! Login to your SMath Studio Forum forum account. New Registrations are disabled.

Offline Davide Carpi  
#2 Posted : 22 February 2014 14:59:35(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,654
Man
Italy
Location: Italy

Was thanked: 1340 time(s) in 879 post(s)
I think that I can add a findvalue function (like the built-in findstr)...
If you like my plugins consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects
thanks 1 user thanked Davide Carpi for this useful post.
on 22/02/2014(UTC)
Offline zorrykid  
#3 Posted : 22 February 2014 19:22:35(UTC)
zorrykid


Rank: Member

Groups: Registered
Joined: 17/02/2014(UTC)
Posts: 18
Italy
Location: Rome

Was thanked: 5 time(s) in 4 post(s)
Thanks a lot for your feedback , in the meantime I have developed something to fulfill this need and to share if .... there is someone else in the same situation.
The function is called Vlookup(M,Val) and is in the snippet attached.
It looks for a value V in the single column vertical array M . The array MUST be sorted . The output values is a 2 values vertical array whose top position gives :

-the position where the values Val has been found if there is an exact match, in this case the bottom
position is at True(=1 means that there is an exact match)
-the position of where the values Val can be inserted if there is not an exact match , in this case the
bottom position is at
False(=0 means that there is not an exact match)

if the Val is not within the extremes of the lookup range there is a message that indicates this situation.
Probably is not developed in the most elegant and updated way but it does it work.

Best regards

Franco
File Attachment(s):
VLookup.sm (13kb) downloaded 46 time(s).
zorrykid attached the following image(s):
sshot-1.png
sshot-2.png
Offline mkraska  
#4 Posted : 22 February 2014 22:07:39(UTC)
mkraska


Rank: Advanced Member

Groups: Registered
Joined: 15/04/2012(UTC)
Posts: 1,990
Germany

Was thanked: 1128 time(s) in 724 post(s)
This might also work:

Vlookup(M,x):findrows(eval(augment(M,range(1,length(M)))),x,1)
Martin Kraska

Pre-configured portable distribution of SMath Studio: https://smath.com/wiki/SMath_with_Plugins.ashx
Offline zorrykid  
#5 Posted : 23 February 2014 02:25:38(UTC)
zorrykid


Rank: Member

Groups: Registered
Joined: 17/02/2014(UTC)
Posts: 18
Italy
Location: Rome

Was thanked: 5 time(s) in 4 post(s)
@Ioan

thanks a lot for your snippet and for your suggestions.
However probably I haven't been very clear in describing what i was trying to replicate.
in the last example :

Val:=2.367

I am not trying to find the closer match of this value with what is reported in the array M'
( that is what is done in your program ) but just the position in which it could be inserted .

So in my program the position 10 and the fact that found is equal to 0 ( i.e is False ) means that the binary search didn't lead to a match but that if this value could be inserted into the array M' the correct position would be the 10th , shifting all the other values downside.

This way I know that in position 10 there is a value that is higher that Val and that If there are other info's linked to the numerical values reported in M' I can obtain this values for instance through an interpolation of the lower and upper value of the interval in which this value is bounded ( and the upper bound is the value at position 10 and the lower one at position 9 )

@Martin

your solution is very elegant and due to my newbieness I haven' fully understood it, so in the next days I have to analyize it better and feel more comfortable with it .
Your approach is very good to understand if something is find in the array and where ..but for what concern the desire to understand where it would be inserted if it was possible ( so for other applications ) is not the best ..but could be useful toghether with other testing in a more elaborate solution.

However another issue I am trying to address is lookup a value when there is a duplication of data in the array . I have seen that my solution doesn't give a consistent result so I am studying deeply this problem and trying to solve it in a more complete way ( is always the desire to emulate completely the VLOOKUP of Excel style )
See this link for a more detailed discussion of this topic.
http://www.sascommunity.org/sug...12%20Drummond.pdfhttp://

Thanks again and best regards.

Franco
Offline mkraska  
#6 Posted : 23 February 2014 13:04:03(UTC)
mkraska


Rank: Advanced Member

Groups: Registered
Joined: 15/04/2012(UTC)
Posts: 1,990
Germany

Was thanked: 1128 time(s) in 724 post(s)
Here is a crude version of VLOOKUP (exact match only, scalar key values only). The parameters are:
key: the value to search for
M: the data table
c.key: the column index where to search for key
c.val: the column index where to get the return value

A plugin version might provide the following features:
- if c.val=0, then the row index of matches is returned. In the attachment this is done by appending a column with corresponding numbers the table. This could be done easily inside the function.
- allow for lists or matrices as key argument. The return value then would be an array of same size and type.
- a flag to choose between exact match closest match and linear interpolation
- a flag to choose extrapolation in case interpolation is choosen (constant or linear extrapolation)
File Attachment(s):
VLookup_kr.sm (30kb) downloaded 49 time(s).
findrows.sm (11kb) downloaded 42 time(s).
mkraska attached the following image(s):
Vlookup.PNG
Martin Kraska

Pre-configured portable distribution of SMath Studio: https://smath.com/wiki/SMath_with_Plugins.ashx
thanks 2 users thanked mkraska for this useful post.
on 23/02/2014(UTC),  on 23/02/2014(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.