Borrar filtros
Borrar filtros

how can i give variable name having 2 number of nested loop(i and j) ?

1 visualización (últimos 30 días)
Vish
Vish el 3 de Feb. de 2016
Editada: Stephen23 el 19 de Jun. de 2019
for i=1:3
for j=1:3
im(i,j)=%action;
end
end
while doing same above code i get error as: "Undefined function or variable 'im'." and warning as: "The Variable 'im' appears to change size on every loop iteration. consider preallocating for speed" here my im variable must b im11,im12,im13,im21....im33 how can i give such variable name in matlab
  6 comentarios
Akbar Khan
Akbar Khan el 4 de Abr. de 2017
It is a good idea to initialize a variable before loop to avoid any warning .. however you code will not result in any error during compilation or execution. However is is always a good c programming practice to initialize variables before loops like
im = zeros(3, 3);
Stephen23
Stephen23 el 4 de Abr. de 2017
Editada: Stephen23 el 19 de Jun. de 2019
"...is always a good c programming practice..."
But this is MATLAB, not C. There is no reason why any "good programming practice" in language X has to be good in language Y.
MATLAB does not require initializing of variables. However preallocating variables is recommended before loops:
"...variable must b im11,im12,im13,im21....im33 how can i give such variable name in matlab"
Don't do this. Use indexing.

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 3 de Feb. de 2016
Don't worry about the warning but don't give unique names to the variables like im11, im12, im13, etc. For more discussion about this bad idea, see the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
If you want to get rid of the warning, preallocation im with
im = ones(3,3);
before the loop.
  1 comentario
Guillaume
Guillaume el 3 de Feb. de 2016
Well actually, do worry about the warning, particularly as it's trivial to avoid:
im = zeros(3, 3);
before the loops.

Iniciar sesión para comentar.

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