Matlab function/code similar to excel vlookup?
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
Cedric
2013-3-26
编辑:Cedric
2013-3-26
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
0 个评论
vivek cheruvu
2016-8-4
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 个评论
Walter Roberson
2016-8-4
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()
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!