简体   繁体   English

左侧和右侧具有不同数量的元素

[英]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 .对于循环索引,我通常使用iijjk

暂无
暂无

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

相关问题 无法执行分配,因为左侧和右侧具有不同数量的元素 - Unable to perform assignment because the left and right sides have a different number of elements 无法执行分配,因为左侧和右侧的元素数量不同。 MATLAB 错误 - Unable to perform assignment because the left and right sides have a different number of elements. MATLAB ERROR Matlab:无法执行赋值,因为左侧和右侧的元素数量不同 - Matlab: Unable to perform assignment because the left and right sides have a different number of elements 尝试定义周期信号的条件时,左侧和右侧具有不同数量的元素 - Left and right sides have a different number of elements when trying to define conditions of a periodic signal MATLAB图中不同的左右轴? - Different right and left axes in a MATLAB plot? MATLAB右侧元素数量不正确 - MATLAB Incorrect number of right hand side elements 使两个向量具有相同数量的元素 - Making Two Vectors Have The Same Number of Elements Matlab中不同位置数的矩阵的每一行的左圆移位 - Left circular shift in Matlab for each row of a matrix of a different number of positions 我如何检查我的5x5矩阵的左上角到右下角内的数字1 - how do i check the number of 1 inside the top left corner to bottom right of my 5x5 matrix 使用Matlab中的image()函数在左侧和右侧显示带有不同标签的图形 - Display a figure with different labels on the left and right side using the image() function in matlab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM