How to do vlookup with several conditions

1 visualizzazione (ultimi 30 giorni)
TingTing
TingTing il 30 Ago 2017
Commentato: TingTing il 5 Set 2017
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
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.

Accedi per commentare.

Risposte (1)

Sailesh Sidhwani
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
  1 Commento
TingTing
TingTing il 5 Set 2017
Thank you very much. That is almost what I want. But precisely I want to do is to find the two lines at the same time, ie I have a matrix of [9 6 12; 9 48 12] and want to get [1;3]. Is that a matrix operation instead of if loop? Thanks again.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by