My ultimate goal is to understand how the code he wrote works on any set of data chosen. The code he wrote is as follow :
load('carsmall')
data = [Horsepower, Weight, Acceleration, MPG];
nVars = size(data,2);
data(any(isnan(data),2), :) = [];
figure('Name', 'carsmall_data')
[sh, ax, ~, hh] = plotmatrix(data);
arrayfun(@(h,lab)ylabel(h,lab),ax(:,1), {'Horsepower','Weight','Accel.','MPG'}')
arrayfun(@(h,lab)xlabel(h,lab),ax(end,:), {'Horsepower','Weight','Accel.','MPG'})
[r,p] = arrayfun(@(h)corr(h.Children.XData(:),h.Children.YData(:)),ax(~eye(nVars)));
arrayfun(@(h,r,p)text(h,min(xlim(h))+range(xlim(h))*.05,max(ylim(h)),...
sprintf('r=%.2f, p=%.3f',r,p),'Horiz','Left','Vert','top','FontSize',8,'Color','r'),...
ax(~eye(nVars)),r,p)
set(sh, 'Marker', 'o','MarkerSize', 2, 'MarkerEdgeColor', ax(1).ColorOrder(1,:))
lsh = arrayfun(@(h)lsline(h),ax(~eye(nVars)));
set(lsh,'color', 'm')
From my part, lets say for the sake of simplicity I have a set of data x=[0.25,0.5,0.75,1,2,3,6,8] and y=[1,1,4,6,1,2,3,5] and I wish to construct the correlation matrix as seen in the code above with these data. I would mimic the code written above for which I have written the following :
data = [x',y'];
nVars = size(data,2);
data(any(isnan(data),2), :) = [];
figure('Name', 'Some Data')
[S, AX, ~, H] = plotmatrix(data);
X=[x' y'];
arrayfun(@(h,lab)ylabel(h,lab),AX(:,1), {'y'})
arrayfun(@(h,lab)xlabel(h,lab),AX(end,:), {'x'})
[r,p] = arrayfun(@(h)corr(X));
arrayfun(@(h,r,p)text(h,min(xlim(h))+range(xlim(h))*.05,max(ylim(h)),...
sprintf('r=%.2f, p=%.3f',r,p),'Horiz','Left','Vert','top','FontSize',8,'Color','r'),...
AX(~eye(nVars)),r,p)
set(S, 'Marker', 'o','MarkerSize', 2, 'MarkerEdgeColor', AX(1).ColorOrder(1,:))
lsh = arrayfun(@(h)lsline(h),AX(~eye(nVars)));
set(lsh,'color', 'm')
The output was this graph :
which is very reasonable but I did not obtain the values of r and p and the regression line for reasons which I want to know why and how to fix it.
The error obtained in this process of execution was the following :
Error using arrayfun
All of the input arguments must be of the same size and shape.
Previous inputs had size 2 in dimension 1. Input #3 has size 1
I would like to also understand more how the arrayfun operates in this case.