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

89 visualizzazioni (ultimi 30 giorni)
MathWorks Support Team
MathWorks Support Team il 27 Set 2019
设定英语情况下,报错信息为: 
??? Subscripted assignment dimension mismatch. 
??? In an assignment A(I) = B, the number of elements in B and 
I must be the same. 

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 12 Set 2021
Modificato: MathWorks Support Team il 30 Dic 2021
此报错发生在:当您尝试将元素分配给现有数组时,元素的大小(维度)与现有数组不兼容(不相等)。例如,以下代码段产生此报错: 
 
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 

Più risposte (0)

Categorie

Scopri di più su MATLAB 快速入门 in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!