필터 지우기
필터 지우기

VLOOK UP Function for MatLab - Closest Match

조회 수: 7 (최근 30일)
Cameron Hudspith
Cameron Hudspith 2023년 10월 16일
댓글: Voss 2023년 10월 16일
Hello, I found the following answer by Jim Riggs which nearly solves a problem I've been having i MatLab. Essentially I need a function which performs this same task, but rather than finding data from col1 which is equal to val1, I need it to find the closest number in col1 to val 1 then return the value from col2.
Any recommendations are helpful!
"
You can make this into a function similar to Vlookup in Excel. For example:
Vlookup = @(data,col1,val1,col2) data((data(:,col1)==val1),col2);
The function Vlookup takes 4 arguments;
  1. data is the table of data
  2. col1 is the column you want to use to perform the lokup
  3. val1 is the value you want to look up (from col1).
  4. col2 is the column that you want to retrieve.
So, in my previous example, this would be
area = Vlookup(data, 1, 12, 2);
Unrecognized function or variable 'data'.
Look up from the "data" table, where column 1 has a value of 12, and return the value from column 2.
"

채택된 답변

Voss
Voss 2023년 10월 16일
col1 = 1;
val1 = 12;
col2 = 2;
[~,idx] = min(abs(data{:,col1}-val1));
result = data{idx,col2};
  댓글 수: 2
Cameron Hudspith
Cameron Hudspith 2023년 10월 16일
That works great. Thank you very much.
Voss
Voss 2023년 10월 16일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by