How to do vlookup with several conditions
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
I have a matrix called comb which has 4 columns. I want to do something like vlookup but with 4 columns. Lets say the four columns correspond to VesselTypeGrp, GTGrp5000,SpeedGroup and RdcCover.
1.0000 1.0000 1.0000 0.7500
2.0000 1.0000 1.0000 0.7500
3.0000 1.0000 1.0000 0.7500
4.0000 1.0000 1.0000 0.7500
5.0000 1.0000 1.0000 0.7500
2.0000 2.0000 1.0000 0.7500
3.0000 2.0000 1.0000 0.7500
4.0000 2.0000 1.0000 0.7500
5.0000 2.0000 1.0000 0.7500
6.0000 2.0000 1.0000 0.7500
I want to find the position (in terms of row) while VesselTypeGrp, GTGrp5000,SpeedGroup and RdcCover are equal to a certain matrix.
Eg
VesselTypeGrp = [2 2 3 3]';
GTGrp5000 = [1 1 1 1]';
SpeedGroup = [1 1 1 1]';
RdcCover = [0.75 0.75]';
What I want to get is [2 2 3 3]' which are the row numbers that such combination will be find in the comb matrix... I know I can do a loop, but I dont want to do that... Is there a matrix solution? TIA.
1 Commento
Jan
il 30 Ago 2017
"I set comb(:,5) to be MuNew_RDC_adj" sound confusing. "equals to some vectors I have (VesselTypeGrp, GTGrp5000,SpeedGroup and RdcCover)" is not clear also. Do we have to know these complicated names to understand the problem? If not, call them "a,b,c,d". According to the description you have 4 vectors and want to compare 5 columns of comb with them. The output should have the same size as VesselTypeGrp, but which size is this?
If you post some code, and explain, that it does not do the job, explain, what happens instead: Do you get an error message or do the results differ from your expectations?
The question is not clear yet. Please edit it an add more details. A small example with some rand value might be useful.
Risposte (1)
Sailesh Sidhwani
il 1 Set 2017
You can use the "find" command in MATLAB to achieve what you are trying to do. Below is a sample example:
A =
9 6 12
33 48 12
9 48 12
>> I = find(A(:,1)==9 & A(:,2)==6 & A(:,3)==12)
I =
1
>> I = find(A(:,1)==9 & A(:,2)==48 & A(:,3)==12)
I =
3
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!