CodeCogs - An iteractive open source Numerical library Welcome... Login
CodeCogs
shopping cart
OSXWindowsLinux
Search CodeCogs
Numerical Components

Valid RSS

ArraySearch

lookup

Available under GPL (Free) and Commercial licence
get a GPL licence
COST (GBP)
this unit 1.87
sub units 0.00
add a commercial licence to your cart
0
viewed 5356 times and licensed 56 times

Performs a lookup on the first data set, returning the value in the second. Excel: LOOKUP

Controller: CodeCogs  Contact Controller
+View version details
Contents hide toc
buy now     get GPL     add to cart

Interface

#include <codecogs/array/search/lookup.h>

using namespace Array::Search;

template<...> B lookup (A value, std::vector<A> lookup, std::vector<B> result, bool sorted=true)
Performs a lookup on the first data set, returning the value in the second. Excel: LOOKUP

Function Documentation

 
template<...> BlookupAvalue
std::vector<A>lookup
std::vector<B>result
boolsorted = true )
This function looks up a value in the given vector. When value is found in the lookup vector, the function returns the corresponding value from the result vector. It is equivalent to the vector form of the Microsoft Excel function LOOKUP. If the parameter sorted is set to true, then this function uses a binary search approach to find the value in lookup. If an exact match cannot be found then it returns the next largest value.
Warning:
The two vectors lookup and result must be of equal length.

Example:

#include <vector>
#include <iostream>
#include <stdio.h>
#include <codecogs/array/search/lookup.h>
 
using namespace std;
 
int main()
{
  vector<double> rowA;
  vector<char> rowB;
 
  rowA.push_back(-2.3);
  rowA.push_back(1.37);
  rowA.push_back(1.54);
  rowA.push_back(2.02);
  rowA.push_back(2.17);
 
  rowB.push_back('a');
  rowB.push_back('b');
  rowB.push_back('c');
  rowB.push_back('d');
  rowB.push_back('e');
 
  cout<<"lookup vector:\n";
  for (unsigned int i=0; i<rowA.size(); i++) cout<<rowA[i]<<"\t";
  cout<<"\n";
 
  cout<<"result vector:\n";
  for (unsigned int i=0; i<rowB.size(); i++) cout<<rowB[i]<<"\t";
  cout<<"\n";
 
  cout<<"lookup 1.37=";
  cout<<Array::Search::lookup<double, char>(1.37, rowA, rowB);
  cout<<"\n";
 
  cout<<"lookup 1.36=";
  cout<<Array::Search::lookup<double, char>(1.36, rowA, rowB);
  cout<<"\n";
 
  cout<<"lookup 1.36 (sort=false)=";
  cout<<Array::Search::lookup<double, char>(1.36, rowA, rowB, false);
  cout<<"\n";
 
  cout<<"lookup 2.01=";
  cout<<Array::Search::lookup<double, char>(2.01, rowA, rowB);
  cout<<"\n";
}
Output:
lookup vector:
-2.3    1.37    1.54    2.02    2.17
result vector:
  a       b       c       d       e
  lookup 1.37=b
  lookup 1.36=a
  lookup 1.36 (sort=false)=?
  lookup 2.01=c
Parameters:
valuethe value to search for in lookup
lookupa vector of values
resulta vector of values
sortedindicated is the data lookup vector is sorted.
Returns:
the result value from the result vector
Authors:
Will Bateman (Sep 2005)
Source Code:

To view or download source code you need either a GPL or Commercial Licence.

buy now     get GPL     add to cart

Not a member, then Register with CodeCogs. Already a Member, then Login.


Page Comments

Format Excel Equations

  You must login to leave a messge


Last Modified: 18 Oct 07 @ 17:07     Page Rendered: 2010-03-14 20:11:28

Valid CSS!   Valid XHTML 1.0 Transitional