how can i give variable name having 2 number of nested loop(i and j) ?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Akbar Khan
le 4 Avr 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
le 4 Avr 2017
Modifié(e) : Stephen23
le 19 Juin 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.
Réponses (1)
Image Analyst
le 3 Fév 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 commentaire
Guillaume
le 3 Fév 2016
Well actually, do worry about the warning, particularly as it's trivial to avoid:
im = zeros(3, 3);
before the loops.
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!