Conditional Scatter plotting based on third data set

7 次查看(过去 30 天)
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:
figure
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 个评论
Wesser
Wesser 2021-6-16
Hi,
I've arrived at a follow up question.
3 0 2 1 4
3 0 2 1 4
2 0 2 1 4
2 1 4 1 4
2 1 4 1 4
2 1 4 2 4
2 1 4 2 4
2 1 4 2 4
2 1 3 2 4
2 1 3 2 4
2 1 3 2 4
2 1 3 2 4
The above data represents where people (columns) have lived over time (rows, years). I'd like to just select the people that have lived ONLY in towns 1, 2 and 3. So if the person lived in towns 1,2, and/or 3 but also towns 0 and/or 4, I do not want to plot thier scatter data. The following will logically tag people who live in towns 1,2 or 3...
communityA = any(town==3,1) | any(town==2,1) | any(town==1,1);
but how do I write the code so that it will logically not select the people (columns) that also have a 4 or 0? I'd like the output to look like the below line for the above example:
communityA = 1 0 0 1 0
Many thanks for you help!!

请先登录,再进行评论。

更多回答(1 个)

dpb
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.

类别

Help CenterFile Exchange 中查找有关 Scatter Plots 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by