Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Akbar Khan
Akbar Khan il 4 Apr 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 il 4 Apr 2017
Modificato: Stephen23 il 19 Giu 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.

Accedi per commentare.

Risposte (1)

Image Analyst
Image Analyst il 3 Feb 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 Commento
Guillaume
Guillaume il 3 Feb 2016
Well actually, do worry about the warning, particularly as it's trivial to avoid:
im = zeros(3, 3);
before the loops.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by