Hi Brett,
As per my understanding, you want to plot the mean and standard deviation of data based on group number at each time point. Additionally you want to overlay this time-course data with some simulations.
- So, I assumed a simple time-course data example with 10 elements and some sample simulation data for better understanding.
time = [1 1 2 2 3 3 4 4 5 5];
group = [1 1 1 2 2 2 3 3 3 4];
data = [10 12 9 11 8 10 7 9 6 8];
Which means,
at time=1, data =10 if it is from group =1 (mapping 1st element in all 3 arrays)
at time=3, data =8 if it is from group =2 (mapping 5th element in all 3 arrays)
uniqueGroups = unique(group);
uniqueTime = unique(time);
meanData = zeros(numel(uniqueGroups), numel(uniqueTime));
stdData = zeros(numel(uniqueGroups), numel(uniqueTime));
- To find the mean and standard deviation you can use mean() and std() , Please refer the following MathWorks Documentation links for more information
for i = 1:numel(uniqueGroups)
for j = 1:numel(uniqueTime)
idx = group == uniqueGroups(i) & time == uniqueTime(j);
meanData(i, j) = mean(data(idx));
stdData(i, j) = std(data(idx));
- To plot mean and standard deviation based on groups at each time point , you can use errorbar as standard deviation is defined as a measure which shows how much variation (such as spread, dispersion, spread,) from the mean exists. For more details, please refer the following MathWorks Documentation link on errorbar-https://in.mathworks.com/help/matlab/ref/errorbar.html
for i = 1:numel(uniqueGroups)
errorbar(uniqueTime, meanData(i, :), stdData(i, :), 'o-', 'LineWidth', 1.5);
title('Mean and Standard Deviation of Time-Course Data');
- To overlay the simulations ,please refer to the following sample code which is using sample simulation data to plot.
simulations = [9.5 11 8.5 10.5 7.5];
timeSimulations = [1 2 3 4 5];
groupSimulations = [1 2 3 4 1];
for i = 1:numel(uniqueGroups)
idx = groupSimulations == uniqueGroups(i);
plot(timeSimulations(idx), simulations(idx), 'r--', 'LineWidth', 1.5);
legend('Group 1', 'Group 2', 'Group 3', 'Group 4','Simulations');
Please execute the MATLAB code to see the plot for the given sample data.