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
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
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
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by