简体   繁体   English

为什么会出现vertcat错误? (Matlab)

[英]Why am I getting a vertcat error? (Matlab)

I am trying to plot the first row of my matrix against time t but I cannot figure out why my matrix yields the error: "vertcat: Dimensions of matrices being concatenated are not consistent." 我试图相对于时间t绘制矩阵的第一行,但是我无法弄清楚为什么矩阵会产生错误:“ vertcat:被连接的矩阵的尺寸不一致。” 在此处输入图片说明

t = linspace(0,100);

y_mat = (1./t).*([1, t+(1/2)*exp(-3*t)-(1/2)*exp(-t); 
(3/2)*(exp(-t)-exp(-3*t)), 1-(3/2)*exp(-3*t)+ 
(1/2)*exp(-t)] * [(t-4)/3;1]);

plot(t,y_mat(1,:))

You are thinking in the term of symbolic notation but implementing in matrix notation. 您在用符号表示法思考,但是在矩阵表示法中实现。 When you do t = linspace(0,100); 当你做t = linspace(0,100); it creates a 1x100 matrix (array). 它创建一个1x100矩阵(数组)。 So when later on it is used in the definition of y_mat , each expression used in the definition evaluates to 1x100 matrix. 因此,稍后在y_mat的定义中使用它时,定义中使用的每个表达式的计算结果为1x100矩阵。 So your y_mat definition is tying to do this : [1x100] * [1 1x100 ; 1x100 1x100] * [1x100 ; 1] 因此,您的y_mat定义就是这样做的: [1x100] * [1 1x100 ; 1x100 1x100] * [1x100 ; 1] [1x100] * [1 1x100 ; 1x100 1x100] * [1x100 ; 1] [1x100] * [1 1x100 ; 1x100 1x100] * [1x100 ; 1] which obviously fails. [1x100] * [1 1x100 ; 1x100 1x100] * [1x100 ; 1]显然失败了。

You have two options: Do all computations in the matrix notation by first computing the matrix multiplication separately and restructuring the matrices to represent the actual multiplication (ensure the 1 s are appropriately replicated). 您有两个选择:以矩阵表示法进行所有计算,方法是首先分别计算矩阵乘法,然后重组矩阵以表示实际乘法(确保正确地复制了1 s)。

OR 要么

use Matlabs's symbolic variables and expressions probably like this : 使用Matlabs的符号变量和表达式可能像这样:

syms t  % creating symbolic variable
% creating symbolic expressions
f0 = 1/t  
f1 = t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
f2 = (3/2)*(exp(-t)-exp(-3*t));
f3 = 1-(3/2)*exp(-3*t)+(1/2)*exp(-t);
f4 = (t-4)/3;
% defining y_mat
y_mat = f0 * [1 f1; f2 f3] * [f4 ; 1]

% putting value in symbolic variable
t = linspace(eps,100); % eps to avoid division by 0 error

% substitute values and evaluate y_mat
y_mat_vals = eval(subs(y_mat));

This gives y_mat_vals a 2x100 matrix, as the answer. 这给y_mat_vals一个2x100矩阵,作为回答。

YOu have messed up your code..you need to be careful when typing such functions. 您弄糟了您的代码..键入此类函数时需要小心。 To make it simple, I have used a loop. 为简单起见,我使用了一个循环。

t = linspace(0,100);

nt = length(t) ;
y_mat = zeros(2,nt) ;

for i = 1:nt
y_mat(:,i) = (1/t(i))*([1           t(i)+(1/2)*exp(-3*t(i))-(1/2)*exp(-t(i));
    (3/2)*(exp(-t(i))-exp(-3*t(i)))   1-(3/2)*exp(-3*t(i))+(1/2)*exp(-t(i))])*[(t(i)-4)/3;1];
end
plot(t,y_mat)

在此处输入图片说明

You can also write it out more explicitly. 您也可以更明确地将其写出。 The equation reads: 等式如下:

[ 1,pt2 ; pt3,pt4 ] * [ pt5 ; 1 ] = [ pt5 + pt2 ; pt3.*pt5 + pt4 ]

Since each of those terms is a scalar, you can compute them for all t at the same time using element-wise multiplication: 由于这些项都是标量,因此您可以使用逐元素乘法同时计算所有t的项:

t = linspace(0,100);

pt2 = t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
pt3 = (3/2)*(exp(-t)-exp(-3*t));
pt4 = 1-(3/2)*exp(-3*t)+(1/2)*exp(-t);

pt5 = (t-4)/3;

y_mat = (1./t) .* [ pt5 + pt2 ; pt3.*pt5 + pt4 ];

plot(t,y_mat)

This might be a bit more verbose, but I don't think it's any less readable than other solutions. 这可能有些冗长,但我认为它的可读性并不比其他解决方案低。 And it is much more efficient: 0.0571 ms, versus 483.3 ms ( syms solution ) and 0.681 ms ( loop solution) , for a t with 500 elements. 而且效率更高:对于500个元素的t ,效率为0.0571毫秒,而483.3毫秒( 符号解 )和0.681毫秒( 循环解)

(Note that multiplying by 1./t uses implicit singleton expansion. This works in MATLAB R2016b and newer. For older versions of MATLAB, use bsxfun .) (请注意,乘以1./t使用隐式单例扩展。这在MATLAB R2016b和更高版本中适用。对于旧版本的MATLAB,请使用bsxfun 。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM