简体   繁体   English

连接的数组的尺寸不一致,但两个数组的大小相同

[英]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.

相关问题 sequenceInputLayer() 被连接的数组的维度不一致 - sequenceInputLayer() Dimensions of arrays being concatenated are not consistent 在MATLAB中调试错误“被连接的数组的维度不一致” - Debugging the error "Dimensions of arrays being concatenated are not consistent" in MATLAB 使用 horzcat 时出错 连接的 arrays 的维度不一致。 Matlab - Error using horzcat Dimensions of arrays being concatenated are not consistent. Matlab bsxfun:所连接矩阵的维数不一致 - bsxfun: Dimensions of matrices being concatenated are not consistent matlab连接的矩阵尺寸不一致 - matlab Dimensions of matrices being concatenated are not consistent 使用垂直时发生错误:串联的矩阵尺寸不一致 - Error using vertical: Dimensions of matrices being concatenated are not consistent 如何解决:使用horzcat时发生错误所连接矩阵的维数不一致 - How to solve: Error using horzcat Dimensions of matrices being concatenated are not consistent 使用CAT的matlab错误,所连接矩阵的维数不一致 - matlab error using CAT, Dimensions of matrices being concatenated are not consistent 使用带有字符的数组时,要级联的矩阵的维数不一致 - Dimensions of matrices being concatenated are not consistent using array with characters 在1x6802 double和6802x1 double数据矩阵中,级联矩阵的尺寸不一致 - Dimensions of matrices being concatenated are not consistent in 1x6802 double and 6802x1 double data matrix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM