How to do vlookup with several conditions

조회 수: 1 (최근 30일)
TingTing
TingTing 2017년 8월 30일
댓글: TingTing 2017년 9월 5일
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
Jan
Jan 2017년 8월 30일
"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.

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

답변 (1개)

Sailesh Sidhwani
Sailesh Sidhwani 2017년 9월 1일
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
TingTing
TingTing 2017년 9월 5일
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.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by