[英]Left and right sides have a different number of elements
When I am trying to execute this code, I get an 'Unable to perform assignment because the left and right sides have a different number of elements.'当我尝试执行此代码时,我得到一个“无法执行分配,因为左侧和右侧有不同数量的元素。” error.
错误。 Where is the problem?
问题出在哪里? Thanks for helping.
感谢您的帮助。
A = 1250;
Q = 450;
t = (0:0.5:10);
y(1) = 0;
for i = 1:length(t)
y(i+1) = y(i) + [(3*(Q/A).*sind(t).^2)-(Q/A)].*0.5;
end
display(y);
Inside the for
loop, you assign an expression (the RHS) to an array (the LHS).在
for
循环中,您将表达式(RHS)分配给数组(LHS)。 y(i)
is a 1x1
array, and the RHS has the same dimensions as t
( 1x21
). y(i)
是一个1x1
数组,RHS 与t
( 1x21
) 具有相同的维度。 You cannot assign 21 values to an array that can only hold one.您不能将 21 个值分配给只能容纳一个的数组。 To repair, I'd suggest preallocating
y
, with the line要修复,我建议预先分配
y
,使用该行
y = zeros(numel(t), numel(t))
or, what I think you probably intended, only call one element of t
at a time或者,我认为您可能打算这样做,一次只调用
t
的一个元素
y(i+1) = y(i) + [(3*(Q/A).*sind(t(i)).^2)-(Q/A)].*0.5;
As an aside, it is bad practice to use i
as a variable as it already has a built-in value in Matlab.顺便说一句,使用
i
作为变量是不好的做法,因为它已经在 Matlab 中具有内置值。 For loop indices I usually use ii
, jj
or k
.对于循环索引,我通常使用
ii
、 jj
或k
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.