Why does MATLAB confuse index variables in a for loop???
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I wrote a program that looked like this.
X=ones(3,6);
for i=1:10000 % high number of iterations needed
X= function(X) % perform some operation on X
emptyness= zeros(3,6);
VonNeumann=(X==[max(X);max(X);max(X)]);
emptyness(VonNeumann)=1;
if max(sum(emptyness')) > 1
for i=1:6
emptyness(:,i)=emptyness(:,i)/sum(emptyness(:,i)); % scaling to 1
end
end
X=(1-1/i)*X + emptyness/i;
%#####
end
Now I've tested the emptyness thing and it did exactly what I wanted. Moreover the program as a whole works fine and I get the convergence results that I want. So I expected there to be nothing wrong with my code.
However when I tried to add a new function at the %##### place I noticed that i=6 always. Clearly this caused big errors. So then I noticed that I was sloppy, I used the index variable 'i' in both for loops. So then I changed the index variable in the emptyness for loop to j, changed this and nothing else. And then suddenly the whole program stopped working correctly!
At this point I have no idea why this is happening to me so please help me!
0 Kommentare
Antworten (4)
Cedric
am 19 Feb. 2013
Bearbeitet: Cedric
am 19 Feb. 2013
You are using the same index variable i in both nested loops. You should rename both differently (e.g. ii and jj) and then use the debug mode to step in your code, checking what happens when/where. For that, put your cursor on the first line of code and press F12 to set a break point; then press F5 to execute and it will stop at the break point. After that, press F10 to step and observe what happens (F11 if you want to step in the function).
5 Kommentare
Cedric
am 19 Feb. 2013
Bearbeitet: Cedric
am 19 Feb. 2013
EDITED code - There is no situation where you want to interfere with the loop index increment mechanism (except when using break) within a loop in MATLAB.
Test e.g. this to understand what happens:
for i = 1:3
fprintf('\nOutter: %d\n',i);
for i = 4:6
fprintf('Inner : %d\n',i);
end
fprintf('Outter: %d\n',i);
end
You'll get
Outter: 1
Inner : 4
Inner : 5
Inner : 6
Outter: 6
Outter: 2
Inner : 4
Inner : 5
Inner : 6
Outter: 6
Outter: 3
Inner : 4
Inner : 5
Inner : 6
Outter: 6
that shows you how the outter/inner loops set a value to i. In your case, as Walter says below, it means that your last line will always be evaluated with i=6 when the conditional statement above it is executed.
Walter Roberson
am 19 Feb. 2013
You should not use the same index variable for nested loops.
When you get to the statemet
X=(1-1/i)*X + emptyness/i;
then if the inner loop was run and the names are nested, then at that point "i" would be the maximum value it was assigned in the inner loop (6) rather than whatever value it "should" have in the outer loop. The outer loop will restore the proper next value for "i" (the change inside the loop will not affect the outer loop). So if it worked when your loops are nested and stops working when you renamed "i" to "j" for the inner loop, the implication is that the code relies on "i" being 6 there sometimes. That could indicate a convergence problem elsewhere in the code, if it was converging by mistake.
Muhammad Adil
am 2 Apr. 2016
Bearbeitet: Walter Roberson
am 2 Apr. 2016
In the following code the idea is that i want to find the derivatives of aa binary sequence repeated untill the bit count reduce to 2; but for first derivative we get 1 bit less but after that same number of bits..... if some one can comment.... i want this result
for alice = [1 1 0 1]
1 1 0 1
0 1 1 (first deri)
1 0 (second deri)
Thanks
close all;
clear all;
clc;
format compact
alice = [1 0 1 1];
der = cell(length(alice)-1, 1);
onz = [];
der{1} = alice;
c = 1;
d=1;
alice_new = [];
in = 0;
% for b = 1:length(alice)-2
len = length(alice);
for b = 1:len-2
c = c+1;
in =0;
for a = 2:1:zz
d = d+1;
in = in+1;
x = xor(alice(d-1), alice(d));
alice_new(1,in)=x
end
der{c}= alice_new;
alice = alice_new;
zz=length(alice_new);
d=1;
end
1 Kommentar
Smith
am 22 Mär. 2020
Still having similar issue here on my system. I did transfer the code from matlab environment to java environment and it work very fine there.
I see that Matlab keep repeating some specific loop counter unnecessarily when it shouldn't even if I renamed the index variable. The counter I use to check it still comes out with some values repeating like
1 2 3 4 5 . . 21601 21601 21602 21602 21603 21603
You can see the manner it keep repeating it's index.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!