Matlab function/code similar to excel vlookup?
Afficher commentaires plus anciens
I am calculating a range of values, x, from a loop and require them to be matched to the corresponding x value from given data and then select the given x value's associated value, y.
Is there a function/code that can do this?
Thanks
Ewan
Réponses (2)
Look at this example:
>> x = [1, 4, 2, 5, 3, 7, 6] ; % Fake x and y.
>> y = x + 10 ;
>> for k = 1 : 7, y(x == k), end
ans =
11
ans =
12
ans =
13
ans =
14
ans =
15
ans =
16
ans =
17
Here, we use logical indexing to extract the relevant element of y, as illustrated below for finding the y corresponding to x equals 5:
>> x == 5
ans =
0 0 0 1 0 0 0
>> class(x)
ans =
double
The test of equality (relational operator) returns a vector of logicals that whose elements indicate whether the test is true (1) or false (0). This vector can be used for indexing y (look at logical indexing in the doc if needed):
>> y(x == 5)
ans =
15
vivek cheruvu
le 4 Août 2016
0 votes
Hello,
I have two sets of data (voltage, energy) each has 4778 values. For every value of energy, I want the corresponding voltage. To be short, I want to implement a lookup table function in Matlab script. These values are used within FOR loop. Please help me out with this, the above code is throwing me error "empty matrix 0x1".
1 commentaire
Walter Roberson
le 4 Août 2016
I just tested Cedric's code as posted and it is working fine.
However, Cedric's code relies upon the value being looked up being bit-for-bit exactly equal to one of the stored x values. If you do not have bit-for-bit exact matches then you need to define how you want the lookup to proceed.
You should probably look at interp1()
Catégories
En savoir plus sur Data Import from MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!