45 views (last 30 days)

Show older comments

I have 4 arrays, A,B,C,D which their size is 80 rows, 4 columns. I want to make a scatter plot between the same column of each two arrays e.g. X=A(:,1),Y=B(:,1) or X=C(:,4), Y=D(:,4). I have a cell array s={s1;s2,...;s16} which s1 is lable for A(:,1), if X=A(:,1) then the lable for x axis is s1 and if Y=A(:,1)then the lable for y axis is s1. I also want to add the regression equation and r-squared value. I used the following code but it's not working. ANy help is appericiated.

s={s1;s2;s3;s4;s5;s6;s7;s8;s9;s10;s11;s12;s13;s14;s15;s16}

% to draw a 1:1 line

fignum = 1;

for i=1:4

while ishandle(fignum)

fignum = fignum + 1;

end

figure(fignum);

X=A(:,i)

Y=C(:,i)

R=corrcoef(X,Y);

R_squared=R(2)^2;

R_squared=R_squared*100

R_squared=round(R_squared)

scatter(X,Y);

%Add least-squares line to scatter plot

h=lsline;

set(h,'color','r');

%Use polyfit to compute a linear regression that predicts y from x

p = polyfit(X,Y,1)

p =spa_sf(p,2)

max_x=max(X);

max_y=max(Y);

min_x=min(X)

min_y=min(Y)

mean_x=mean(X)

mean_y=mean(Y)

if (max_x > max_y)

max_value=max_x;

else

max_value=max_y;

end;

if X==A(:,i)

label_x=s(i);

else if X==B(:,i)

label_x=s(i+4);

else if X==C(:,i)

label_x=s(i+8);

else if X==D(:,i)

label_x=s(i+12);

end

if Y==A(:,i)

label_y=s(i);

else if Y==B(:,i)

label_y=s(i+4);

else if Y==C(:,i)

label_y=s(i+8);

else if Y==D(:,i)

label_y=s(i+12);

end

xlabel(label_x); ylabel(label_y);

xlim([0 max_value+max_value/20]);

ylim([0 max_value+max_value/20]);

line([0,max_value+max_value/20],[0,max_value+max_value/20],... 'linewidth',1,... 'color',[0,0,0]);

text(mean_x-mean_x/1.1, max_y-(max_y/4), ['R^2 (%) = ' num2str(R_squared)])

text(mean_x-mean_x/1.1, max_y-(max_y/4)-(max_y/10), ['Y = ' num2str(p(1))])

text(mean_x-mean_x/1.1+max_x/8, max_y-(max_y/4)-(max_y/10), [' X + ' num2str(p(2))])

end

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

Start Hunting!