为什么我获得报错信息​“无法执行赋值,因为​左侧的索引与右侧的大​小不兼容。”?

254 次查看(过去 30 天)
MathWorks Support Team
设定英语情况下,报错信息为: 
??? Subscripted assignment dimension mismatch. 
??? In an assignment A(I) = B, the number of elements in B and 
I must be the same. 

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2021-12-30
此报错发生在:当您尝试将元素分配给现有数组时,元素的大小(维度)与现有数组不兼容(不相等)。例如,以下代码段产生此报错: 
 
A = [1 2 3; 4 5 6]; 
B = [7 8 9 10]; 
A(2,:) = B 
 
其中,A是2乘3的矩阵,B是1乘4的矩阵。代码尝试将B赋值给A的第二列,此时等号右侧B有4个元素,等号左侧A的第二列有3个元素,个数不相等引起报错。为了正常运行,等号右侧也应该是3个元素,例如: 
 
B = [11 12 13]; 
A(2,:) = B 
 
更详细的说明请参考: 
https://www.mathworks.com/help/matlab/math/array-indexing.html 
源链接:www.mathworks.com/matlabcentral/answers/93586-why-do-i-get-the-subscripted-assignment-dimension-mismatch-error-message 

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!