Clear Filters
Clear Filters

Conditional Scatter plotting based on third data set

9 views (last 30 days)
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!!

Accepted Answer

Joel Lynch
Joel Lynch on 9 Jun 2021
Edited: Joel Lynch on 9 Jun 2021
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 Comments
Wesser
Wesser on 16 Jun 2021
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!!
Joel Lynch
Joel Lynch on 16 Jun 2021
Edited: Joel Lynch on 16 Jun 2021
Try
communityA = all(town<4,1) & all(town>0,1);

Sign in to comment.

More Answers (1)

dpb
dpb on 9 Jun 2021
>> 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.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!