getting Subscripted assignment dimension mismatch.

8 visualizaciones (últimos 30 días)
wong wei kher
wong wei kher el 13 de Oct. de 2014
Editada: wong wei kher el 13 de Oct. de 2014
my codes are as follow :
when i use this , the loop works
for i=1:3
vec=(1+i):(2+i);
mat(i,:)=vec;
vec=[];
end
EDU>> mat
mat =
2 3
3 4
4 5
but when i use this , it shows the error message Subscripted assignment dimension mismatch.
EDU>> for i=1:3
vec=(1+i):(3+i);
mat(i,:)=vec;
vec=[];
end
Subscripted assignment dimension mismatch.
EDU>> for i=1:5
vec=(1+i):(3+i);
mat(i,:)=vec;
vec=[];
end
Subscripted assignment dimension mismatch.
how can i rectify this ? i need this concept for a huge loop and it shows similar error message when i run my code the matrix generally runs until 5th or 6th row
HELP PLEASE !!!
  1 comentario
Stephen23
Stephen23 el 13 de Oct. de 2014
Editada: Stephen23 el 13 de Oct. de 2014
Do NOT use i as the loop variable name. This is already the name of the inbuilt imaginary unit . Overriding the inbuilt variable can create difficult-to-fix errors and can be slower to resolve. Using i makes it hard to search for the variable name. Use ii or kk instead.

Iniciar sesión para comentar.

Respuesta aceptada

Orion
Orion el 13 de Oct. de 2014
Editada: Orion el 13 de Oct. de 2014
Hi,
if you do
for i=1:3
vec=(1+i):(2+i);
mat(i,:)=vec;
vec=[];
end
for i=1:3
vec=(1+i):(3+i);
mat(i,:)=vec;
vec=[];
end
the you get an error.
You need to clear your workspace or define the mat variable before the loop
for i=1:3
vec=(1+i):(2+i);
mat(i,:)=vec;
vec=[];
end
clear mat
for i=1:3
vec=(1+i):(3+i);
mat(i,:)=vec;
vec=[];
end
The error occurs because after the first loop, mat is a matrix with 2 columns.
And in the second loop, you use a 3 element vector vec to fill the line.
so the dimensions mismatch.
Note : you should put your question in code form, so it is more readable for everyone.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by