How to get values from a column based on the values of other columns?
조회 수: 7 (최근 30일)
이전 댓글 표시
Hi,
I ordered a set of data as in the example below:
A B C D E F G J
,1985,04,10,1800,4,140,1, 2.0,
,1985,04,11,0900,4,999,1, 0.0,
,1985,07,09,0300,4,999,1, 0.0,
,1985,04,11,2100,4,020,1, 5.0,
,1987,03,03,1000,4,360,1, 10.0,
,1987,03,03,1100,4,360,1, 10.0,
,1987,01,03,0700,4,360,1, 8.0,
,1987,01,03,0900,4,360,1, 7.0,
,1987,03,03,1200,4,360,1, 10.0,
,1987,03,03,1300,4,360,1, 10.0,
,1985,07,09,1500,4,180,1, 10.0,
,1985,07,09,1800,4,180,1, 11.0,
,1985,07,10,0600,4,160,1, 3.0,
,1985,07,10,1000,4,180,1, 4.0,
,1985,04,11,1500,4,360,1, 9.0,
,1985,04,11,2000,4,050,1, 5.0,
,1985,07,10,1200,4,180,1, 5.0,
,1985,07,10,1500,4,180,1, 6.0,
,1987,01,03,0600,4,360,1, 8.0,
,1987,01,03,1100,4,360,1, 6.0,
,1985,07,09,0600,4,999,1, 0.0,
,1985,07,09,1200,4,180,1, 6.0,
,1987,01,03,1300,4,360,1, 6.0,
,1987,01,03,1600,4,360,1, 7.0,
,1987,01,03,1900,4,360,1, 6.0,
,1987,03,03,0500,4,020,1, 8.0,
,1987,03,03,0900,4,360,1, 10.0,
,1987,01,03,1400,4,320,1, 5.0,
,1987,03,03,1500,4,360,1, 10.0,
I want to create an array with the values in column "J" based on the values in column "B". . For example the vector Z should contain only the values of "J" that are on the same row with the value "01" in column "B", vector X only the "03" and so on ...
Thanks in advanace for the help and suggestions.
댓글 수: 0
채택된 답변
Michael Haderlein
2014년 8월 27일
Are A...J cell arrays with strings or numeric arrays?
If you have numbers, this should do the job:
Z=J(B==1);
If it's cell arrays with strings, you need:
Z=J(strcmp(B,'01'));
Best regards,
Michael
추가 답변 (1개)
Joseph Cheng
2014년 8월 27일
편집: Joseph Cheng
2014년 8월 27일
you can use == to find the rows for each B value. here is an example of an implementation
uB = unique(MatAnsExample(:,2));
for ind = 1:length(uB)
Result(ind).B = uB(ind);
Result(ind).J = MatAnsExample(MatAnsExample(:,2)==uB(ind),8);
end
here i find each unique instance of values within column B. then with the for loop i store the current B in the Result structure as well as the values of J that correspond with that B. to do this i generate the logical mask of when column B is equal to the B in question.
댓글 수: 4
Joseph Cheng
2014년 8월 27일
so if you run my code lets say with an adaptation
uB = unique(MatAnsExample(:,2));
for ind = 1:length(uB)
Result(ind).B = uB(ind);
Result(ind).J = MatAnsExample(MatAnsExample(:,2)==uB(ind),8);
disp(['For a B of: ' num2str(uB(ind)) ' the J column are: ' num2str(Result(ind).J')]);
end
you get the results
For a B of: 1 the J column are: 8 7 8 6 6 7 6 5
For a B of: 3 the J column are: 10 10 10 10 8 10 10
For a B of: 4 the J column are: 2 0 5 9 5
For a B of: 7 the J column are: 0 10 11 3 4 5 6 0 6
you get the examples you just wrote. so instead of using individual variables Z, X, Y, etc. for each value of B. I am storing it as Result(N) where N from 1 to the number of unique instances within the B column. So to get the Z, X, etc. variable you can simply just type in Result(1).J instead. I also just put it in this form as i don't know what you are doing. The Right side of the equation is what you should be interested in and not the left side. The left side is for you to determine.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!