我有以下代码:
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
说明/问题:
binCombos
是一个二维元胞数组,每个元胞具有不均匀数量的数据点。我正在为每个唯一单元格的数据拟合一条二次曲线,并尝试(不成功)输出R^2 值并绘制残差图。
我认为问题与函数所需的“间隔”与尝试查找 rsquare 时polyval
的数组大小不匹配有关,同样用于计算残差。y_vrbl(thisBin)
例如,如果我设置interval = x_vrbl(thisBin)
残差“工作”,但 polyfit 都搞砸了。