How to find mean and errorbars for scatter plot at various x values.

25 次查看(过去 30 天)
I have created a scatter plot from 2 matrices of the same size (501x1001) using the code
figure
scatter(Mat1(:),Mat2(:),'.');
Mat1 contains values at set intervals of 0.25 from 0 to 3, Mat2 contains many different decimal values.
I has created a scatter plot which looks like this (note, this is 4 different y matrices plotted on the same figure against the x axis), however I would like to know how I can use this to find the mean values for each of my 4 matrices and the corresponding errorbars to create more of a line graph with 4 lines and their corresponding errorbars, any help would be massively appreciated.
Thank you.

回答(2 个)

William Rose
William Rose 2022-9-4
I don't understand the organization of Mat1 and Mat2. There are 13 x-values repeated many times among the 501,501 elements of Mat1? There are many y-values for every x value (501501/13?). I need to know how the arrays are structured, in order to find the mean and SD at each x-value. Can you represent the data as
x=0:.25:3;
y1=randn(501,13);
y2=0.5+randn(501,13)*1.5;
y3=-0.5+randn(501,13)*2;
%Next findmean and SD ateach x-value
y1m=mean(y1);
y1sd=std(y1);
y2m=mean(y2);
y2sd=std(y2);
y3m=mean(y3);
y3sd=std(y3);
%plot results
figure;
errorbar(x,y1m,y1sd,'r')
hold on;
errorbar(x,y2m,y2sd,'g')
errorbar(x,y3m,y3sd,'b')
legend('y1','y2','y3')
That would be much more compact for the x-values, and easier to handle.
Good luck!
  1 个评论
Charlie Milford
Charlie Milford 2022-9-4
My 'x' values in 'Mat1' are also a matrix of the same size of all my other 'y' or Mat2 matrices. But all values in the x matrix are from 0 to 3 in 0.25 intervals.
I think what I have done here has worked:
meandif=[0:0.25:3];
for i=1:13
a=find(Stanprog_region==meandif(i));
means=nanmean(PDdifference10(a));
stds=nanstd(PDdifference10(a));
meandif10(i)=means;
stddif10(i)=stds;
end
%Where Stanprog_region=Mat1 and PDdifference10=Mat2
Thanks for your help.

请先登录,再进行评论。


dpb
dpb 2022-9-4
The specific answer is
M1=mean(Mat2,'all');
S1=std(Mat2,[],'all');
general answer is to not create multiple sequentially-named variables but use either a 3D array where each is by plane or a cell array where each cell holds the ith 2D array. Then you can write generic code that iterates over the array.
The x value for plotting would then be just be the single x vector.
  3 个评论
dpb
dpb 2022-9-4
Well, really couldn't say without knowing how the data were stored/content of the variables and what actually was intended to compute, but if it looks about right, it probably is.
You can always create small-enough arrays or use subsets of the given arrays that are small enough you can see precisely what data you have and manually check get the answers expected.
Charlie Milford
Charlie Milford 2022-9-4
This is what I produced which seems to be the trend I was looking for. Thanks a lot for your help!

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by