Matlab function/code similar to excel vlookup?

3 ビュー (過去 30 日間)
Ewan
Ewan 2013 年 3 月 26 日
コメント済み: Walter Roberson 2016 年 8 月 4 日
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

回答 (2 件)

Cedric
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

vivek cheruvu
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
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 ExchangeData Import from MATLAB についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by