简体   繁体   English

我的代码运行,有时会出现错误“使用horzcat时出错,所连接的矩阵维数不一致。”

[英]My code runs then sometimes it has error “Error using horzcat Dimensions of matrices being concatenated are not consistent.”

This is my code and I understand you have to have dimensions of matrix equal to line up which is why I transpose y to make it a column and both have 21 rows but sometimes the code runs when I publish but then after one time of running it persistently then hits this error if I try to run it again. 这是我的代码,我理解您必须具有等于矩阵的矩阵尺寸,这就是为什么我将y转置为一列并且都具有21行的原因,但是有时代码在我发布时运行,但是在运行了一次之后如果我尝试再次运行它,就会持续出现此错误。 It says error on line disp([t,y]) 它说在线disp([t,y])上的错误

%Define parameters         
A=1250; %m^2        
Q=450; %m^3/d       
a=150;        
y(1)=0;        
t=[0:0.5:10]';          
y=y';          
disp('        t            y')          
disp('----------------------')            
disp([t,y])           
%Set loop in (Eulers method) for the amount of steps we want to do to
%find our estimated solution       
for i=1:20           
y(i+1)=y(i)+((3*(Q/A)*sin(t(i)).^2)-(a*(1+y(i)).^1.5/A))*0.5;             
end             
%generate plot for data           
plot(t,y)          
title('Plot of y vs t')               
xlabel('time')              
ylabel('y')                 

Your problem is that y only has one element when you attempt to display it. 您的问题是,当您尝试显示y时,它只有一个元素。

You're constructing [t,y] which is a concatenation of two elements - t and y . 您正在构造[t,y] ,它是两个元素ty的串联。 But t is a vector of some length (21), whereas you have not initialized y to be anything. 但是t是某个长度的向量(21),而您尚未将y初始化为任何东西。

If you really do want to display y before it's been set, you can initialize it to zero using y(1:21) = 0 如果您确实想在设置之前显示y ,则可以使用y(1:21)= 0将其初始化为零。

But why do you want to display y and t before you've calculated your estimated solution, anyway? 但是,无论如何,为什么要在计算估计解之前显示yt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决:使用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 使用垂直时发生错误:串联的矩阵尺寸不一致 - Error using vertical: Dimensions of matrices being concatenated are not consistent 我如何处理“使用vertcat的错误连接矩阵的尺寸在Matlab中不一致”? - How do i handle “Error using vertcat Dimensions of matrices being concatenated are not consistent” in Matlab? Matlab代码崩溃并给出错误:所连接矩阵的维数不一致 - Matlab code crashes and gives error: Dimension of matrices being concatenated are not consistent 使用带有字符的数组时,要级联的矩阵的维数不一致 - Dimensions of matrices being concatenated are not consistent using array with characters bsxfun:所连接矩阵的维数不一致 - bsxfun: Dimensions of matrices being concatenated are not consistent matlab连接的矩阵尺寸不一致 - matlab Dimensions of matrices being concatenated are not consistent horzcat错误:尺寸不一致 - horzcat error: demensions not consistent 在MATLAB中调试错误“被连接的数组的维度不一致” - Debugging the error "Dimensions of arrays being concatenated are not consistent" in MATLAB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM