how can i give variable name having 2 number of nested loop(i and j) ?
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
Akbar Khan
2017 年 4 月 4 日
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
2017 年 4 月 4 日
編集済み: Stephen23
2019 年 6 月 19 日
"...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.
回答 (1 件)
Image Analyst
2016 年 2 月 3 日
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 件のコメント
Guillaume
2016 年 2 月 3 日
Well actually, do worry about the warning, particularly as it's trivial to avoid:
im = zeros(3, 3);
before the loops.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!