How to get values ​​from a column based on the values ​​of other columns?

18 次查看(过去 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.

采纳的回答

Michael Haderlein
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
  2 个评论
Nym
Nym 2014-8-27
Thank You, I tried but I dont get the correct values... I'l try some more
Nym
Nym 2014-8-29
In the end the problem was with the dataset now I have the correct answer. Thanks for the help.
As a side note I tried the other answers once I realized the error and are right now... Thank you all

请先登录,再进行评论。

更多回答(1 个)

Joseph Cheng
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 个评论
Nym
Nym 2014-8-27
Thank you but I fail to adapt to my needs suggested code, my knowledge of MATLAB is poor.
Joseph Cheng
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!

Translated by