Conditional Scatter plotting based on third data set

조회 수: 13(최근 30일)
Wesser 2021년 6월 9일
편집: Joel Lynch 2021년 6월 16일
The b[e]low matrix represents 5 people (columns) over time (each row is a year). I'd like to generate some script that, returns a vector, and says something along the lines of if the entire column is 1 (male), then that person is designated as 1, if the entire column is 3 (female that has never breastfed), then that person is designated as 3, if a column is a mix of 2 (female that has breastfed at some point) and 3, then designate that person at 2.
3 1 2 1 3
3 1 2 1 3
2 1 2 1 3
2 1 2 1 3
2 1 2 1 3
2 1 2 1 3
2 1 2 1 3
2 1 3 1 3
2 1 3 1 3
2 1 3 1 3
2 1 3 1 3
2 1 3 1 3
I'd like to have a vector returned that looks like this for the above example
sex = 2 1 2 1 3
Then, I would like to use this to add (NOT DONE YET) a condition to the following plot:
scatter (Resident , MC_participant_Mean) %plot of duration of residency vs mean start of exposure
xlabel('Duration of residence in community (yr)')
ylabel('Mean start of Exposure after 1970 (yr)')
title('Start of exposure vs. years in community')
So if a person has a sex status of 1 (male), then plot their {scatter (Resident , MC_participant_Mean) } data points as green; if the sex status is 2, then red; if 3, then blue....I'd like to color-code the scatter plot based on the sex status of the study participant. Is this possible??
Thank you for your time and help!!

채택된 답변

Joel Lynch
Joel Lynch 2021년 6월 9일
편집: Joel Lynch 2021년 6월 9일
Assuming columns with 1's will only have 1's, and columns with 2's and 3's will never have 1's, then it is as simple as taking the minimium along the first dimension (the 1 in the thrid argument of min())
sex = min( data,[],1);
The second part depends on how Resident and MC_participant_Mean are structured. The easiest approach is to issue three scatter plot commands, logically indexed based on sex.
hold on
scatter (Resident(idx1) , MC_participant_Mean(idx1),[],'k') % note the [] is the size of the scatter
scatter (Resident(idx2) , MC_participant_Mean(idx2),[],'r')
scatter (Resident(idx3) , MC_participant_Mean(idx3),[],'b')
Where idx1/idx2/idx3 are obtained by logical operations.
So, if your first matrix has the same number of columns as the length of Resident and MC_participant_Mean, then you would define them (before plotting of course) by
idx1 = sex==1;
idx2 = sex==2;
idx3 = sex==3;
Of course, you could just drop in these equalities into the plot line commands.
This won't work if Resident or MC_participant_Mean are multidimensional, or don't correspond to the columns in the first matrix.
  댓글 수: 3
Joel Lynch
Joel Lynch 2021년 6월 16일
communityA = all(town<4,1) & all(town>0,1);

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

추가 답변(1개)

dpb 2021년 6월 9일
>> SX=1*all(M==1)+2*(any(M==2)&any(M==3))+3*all(M==3)
SX =
2 1 2 1 3
Create categorical variable from the coding; choose names at will--
>> SX=categorical(SX,[1:3],{'Male','Female YES','Female NO'})
SX =
1×5 categorical array
Female YES Male Female YES Male Female NO
You can then use logical addressing to select appropriate subsets and plot as desired.

Community Treasure Hunt

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

Start Hunting!

Translated by