[英]Dimensions of arrays being concatenated are not consistent but both arrays have the same size
I'm trying to perform the least-squares regression to check whether the optimal parameters of my distribution are approximately equal to the ones used to generate the random numbers.我正在尝试执行最小二乘回归来检查我的分布的最佳参数是否大约等于用于生成随机数的参数。
I generated a probability density function on a histogram obtained by taking random points from the same distribution and I am trying to perform least-squares regression but I am having problems with dimension inconsistency.我在通过从同一分布中获取随机点获得的直方图上生成了一个概率密度函数,并且我正在尝试执行最小二乘回归,但我遇到了维度不一致的问题。
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)
When I try to run this, I get an error stating当我尝试运行它时,我收到一条错误消息
Dimensions of arrays being concatenated are not consistent.
But when I check y_ and x_, both have the same size.但是当我检查 y_ 和 x_ 时,它们的大小相同。 What is the problem?
问题是什么?
In your code在您的代码中
H = [ones(length(y_),1),x_]; % This is the problem
the statement ones(length(y_),1)
is creating a 49x1 column vector, while x is a 1x49 row vector.语句 one
ones(length(y_),1)
正在创建一个 49x1 的列向量,而 x 是一个 1x49 的行向量。 So you need to one or the other of the below to concatenate depending on whether you want a 2-row or 2-column matrix因此,您需要根据需要 2 行或 2 列矩阵来连接以下其中一项
H = [ones(length(y_),1),x_']; % creates a 49x2
H = [ones(1,length(y_)),x_]; % creates a 2x49
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.