How to find mean and errorbars for scatter plot at various x values.
12 次查看(过去 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.
0 个评论
回答(2 个)
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!
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
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Errorbars 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!