Matlab function/code similar to excel vlookup?

조회 수: 4 (최근 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 CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by