I have the following code:
colBIN = {0.050, 0.055, 0.060, 0.065, 0.070, 0.075, 0.080, 0.085, 0.090, 0.095,0.1};
for i = 1 : length(colBIN)-1
colBIN{i,2} = find(cols(:,1) <= cell2mat(colBIN(i+1,1)) & cols(:,1) > cell2mat(colBIN(i,1)));
end
rowBIN = {0.045, 0.046, 0.047, 0.048, 0.049, 0.050, 0.051, 0.052};
for i = 1 : length(rowBIN)-1
rowBIN{i,2} = find(rows(:,1) <= cell2mat(rowBIN(i+1,1)) & rows(:,1) > cell2mat(rowBIN(i,1)));
end
binCombos = cell(length(rowBIN)-1,length(colBIN)-1);
for m = 1 : length(rowBIN)-1
for n = 1 : length(colBIN)-1
binCombos{n,m} = intersect( rowBIN{m,2}(:,1),colBIN{n,2}(:,1));
end
end
binRows = size(binCombos,1);
binCols = size(binCombos,2)-1;
j = j + 1;
for n = 1 : binRows;
for m = 1 : binCols;
thisBin = binCombos{n,m}(:,:);
if isempty(thisBin)==0
%polyfit
quadmod = polyfit(x_vrbl(thisBin), y_vrbl(thisBin), 2);
interval = 0.0:0.001:1;
quadmodcurve = polyval(quadmod,interval);
[r2 rmse] = rsquare(y_vrbl(thisBin), quadmodcurve);
plot(x_vrbl(thisBin), y_vrbl(thisBin), '*', interval, quadmodcurve);
xlabel('x_vrbl');
ylabel('y_vrbl');
axis([0,1,0,1]);
header = ['R^2 =' num2str(r2),'coeffs:',num2str(quadmod)];
title(header);
saveas(gcf, sprintf('plot_%d.pdf', j));
%residuals
res = y_vrbl(thisBin) - quadmodcurve;
plot(x_vrbl(thisBin),res,'+');
header2 = ['residuals'];
title(header2);
saveas(gcf, sprintf('residuals_%d.pdf', j));
end
j = j + 1;
end
end
Explanation/Problem:
binCombos
is a 2 dimensional cell array and each cell has a non-uniform number of data points. I am fitting a quadratic curve to the data for each unique cell, and trying (unsuccessfully) to output the R^2 value and also plot residuals.
I think the problem is related to the fact that the 'interval' required for the polyval
function does not match the array size for y_vrbl(thisBin)
when trying to find rsquare, and likewise for calculating the residuals. For example, if I set interval = x_vrbl(thisBin)
then the residuals "work" but the polyfit is all messed up.