Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

for loop execution error

1 visualización (últimos 30 días)
Shahrukh s
Shahrukh s el 25 de Nov. de 2020
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
iter1 = 1:9;
y_div_1 = 4; y_div_2 = 5; y_div_3 = 3; y_div_4 = 6; y_div_5 = 3; y_div_6 = 4; y_div_7 = 5; y_div_8 = 5; y_div_9 = 4;
x_div_1 = 4; x_div_2 = 5; x_div_3 = 3; x_div_4 = 6; x_div_5 = 3; x_div_6 = 4; x_div_7 = 5; x_div_8 = 5; x_div_9 = 4;
inodes = 1
for iter1 = 1:9
for i_y = 1:eval(['y_div_' num2str(iter1) '+ 1'])
for i_x = 1:eval(['x_div_' num2str(iter1) '+ 1'])
eval(['NODES' num2str(iter1) '(inodes,1) = 10000 + x_div_' num2str(iter1) '* (i_y - 1) + i_x'])
inodes = inodes + 1;
end
end
end
what is the issue with this code why it is not working?

Respuestas (1)

Image Analyst
Image Analyst el 25 de Nov. de 2020
While the original code
iter1 = 1:9;
y_div_1 = 4; y_div_2 = 5; y_div_3 = 3; y_div_4 = 6; y_div_5 = 3; y_div_6 = 4; y_div_7 = 5; y_div_8 = 5; y_div_9 = 4;
x_div_1 = 4; x_div_2 = 5; x_div_3 = 3; x_div_4 = 6; x_div_5 = 3; x_div_6 = 4; x_div_7 = 5; x_div_8 = 5; x_div_9 = 4;
inodes = 1
for iter1 = 1:9
for i_y = 1:eval(['y_div_' num2str(iter1) '+ 1'])
for i_x = 1:eval(['x_div_' num2str(iter1) '+ 1'])
eval(['NODES' num2str(iter1) '(inodes,1) = 10000 + x_div_' num2str(iter1) '* (i_y - 1) + i_x'])
inodes = inodes + 1;
end
end
end
runs, it is bad for at least 2 reasons. The first line is not needed at all, and you use eval(). Also, you don't need to have a bunch of separate variables when you can just use an array. Also not sure why you're adding 1 which causes it to go beyond the end of the array. Corrected code (I think, though I don't know what you want to do) is:
y_div = [4; 5; 3; 6; 3; 4; 5; 5; 4];
x_div = [4; 5; 3; 6; 3; 4; 5; 5; 4];
nodeCount = 1;
for iter1 = 1: length(x_div)
fprintf('Iteration %d.\n', iter1);
for i_y = 1:y_div(iter1)
for i_x = 1:x_div(iter1)
NODES{iter1, nodeCount} = 10000 + x_div(iter1) * (i_y - 1) + i_x;
nodeCount = nodeCount + 1;
end
end
end
fprintf('nodeCount = %d.\n', nodeCount);

Community Treasure Hunt

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

Start Hunting!

Translated by