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

조회 수: 25 (최근 30일)
Charlie Milford
Charlie Milford 2022년 9월 4일
댓글: Charlie Milford 2022년 9월 4일
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