我正在嘗試執行最小二乘回歸來檢查我的分布的最佳引數是否大約等于用于生成亂數的引數。
我在通過從同一分布中獲取隨機點獲得的直方圖上生成了一個概率密度函式,并且我正在嘗試執行最小二乘回歸,但我遇到了維度不一致的問題。
N = 10000;
mu = 5; sigma = 2;
r = randn(N,1);
x = mu sigma*r;
bin=mu-6*sigma:0.5:mu 6*sigma;
[~,centers]=hist(x,bin);
f=hist(x,bin);
plot(bin,f,'bo'); hold on;
xlabel('bin');
ylabel('f');
y_ = f;
x_ = bin;
H = [ones(length(y_),1),x_]; % This is the problem
% Least-Squares Regression
Astar = inv(H'*H)*H'*y_;
Ytilde = H*Astar;
plot(x_,Ytilde, 'r-','LineWidth',2)
當我嘗試運行它時,我收到一條錯誤訊息
Dimensions of arrays being concatenated are not consistent.
但是當我檢查 y_ 和 x_ 時,它們的大小相同。問題是什么?
uj5u.com熱心網友回復:
在您的代碼中
H = [ones(length(y_),1),x_]; % This is the problem
該陳述句ones(length(y_),1)正在創建一個 49x1 列向量,而 x 是一個 1x49 行向量。因此,您需要根據需要 2 行或 2 列矩陣來連接以下其中一項
H = [ones(length(y_),1),x_']; % creates a 49x2
H = [ones(1,length(y_)),x_]; % creates a 2x49
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/486173.html
