【发布时间】:2017-11-16 00:19:30
【问题描述】:
我有以下代码:
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 值以及 绘制残差。
我认为问题与以下事实有关:polyval 函数所需的“间隔”在尝试查找 rsquare 时与 y_vrbl(thisBin) 的数组大小不匹配,同样用于计算残差。例如,如果我设置了interval = x_vrbl(thisBin),那么残差“起作用”,但 polyfit 全部搞砸了。
【问题讨论】:
-
据我所知,无法运行此代码,它应该是正确的,你最初的做法。究竟是什么问题?您收到错误消息吗?
-
再看一遍:rsquare 是做什么的?你能发布你的实现吗?
-
这是我用于 r^2 的例程:mathworks.com/matlabcentral/fileexchange/…
标签: matlab for-loop regression