Vlookup equivalent function for matlab
134 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jung
am 16 Dez. 2019
Kommentiert: Jim Riggs
am 16 Okt. 2023
Hello,
I have two tables (table 1: 241x2 and table 2: 241x35).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/254697/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/254698/image.jpeg)
I would like to use something similar to vlookup to find the subbasin area that matches the subbasin #. I have 241 subbasins.
Would appreciate your help!
0 Kommentare
Akzeptierte Antwort
Jim Riggs
am 16 Dez. 2019
Bearbeitet: Jim Riggs
am 17 Dez. 2019
If your data is in a 2D table (matrix) and you want to locate a column that matches the exact value from another column, then logical indexing is a natural way to go.
Suppose data(:,1) represents the column of subbasin number, and data(:,2) represents the column of area, then to select an area for a given subbasin number
subbasin_no = 12;
area = data((data(:,1)==subbasin_no),2);
"area" contains every value from column 2 where column 1 = subbasin_no.
Note that this is more versatile than VLOOKUP in Excel.
1) The data does not have to be monotonically increasing, as with VLOOKUP
2) You can use logical indexing based on any column (with VLOOKUP, you must perform the look-up based on column 1)
3) It can return multiple values.
2 Kommentare
Jim Riggs
am 17 Dez. 2019
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;
- data is the table of data
- col1 is the column you want to use to perform the lokup
- val1 is the value you want to look up (from col1).
- col2 is the column that you want to retrieve.
So, in my previous example, this would be
area = Vlookup(data, 1, 12, 2);
Look up from the "data" table, where column 1 has a value of 12, and return the value from column 2.
Jim Riggs
am 16 Okt. 2023
No problem.
Just find the distance from each value to the value you wamt (i.e. subtract the target value from each data point) then take the absolute value of each, and find the minimum value. This is the one closest to the desired value.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Tables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!