Around line 62 or so you have
Within that for loop, around line 140 or so you have
if m ==1
for j = 1:nos
for n = 1:now
for i=1:noc
Permf(n,i)=P(j,i);
end
end
end
end
Notice that you have the for n inside the for n . MATLAB does define what happens when you do this: the change to n will be in effect until the next outer for n starts, at which point n will be set to the "next" value of the outer loop as-if the change to n had not been done. But meanwhile until the bottom of the loop, n has changed. In particular at your line just inside the inner while loop you are having problems with,
functiontemp=Ftotal(n,m);
the n that is going to be used there is the n that was left-over from the inner for n loop, not the n associated with the outer for n loop.
Notice as well that the assignment to Permf is inside a triple for loop but is assigning to a variable using only two indices. The value that Permf(n,i) is going to be left with is going to be the last value assigned to it. You could achieve the same effect with
Permf(1:now, 1:noc) = P(nos, 1:noc)
and since you are not initializing Permf (growing it inside the loop) and it is always the same size, this in turn could be expressed as
Permf = repmat(P(nos, :), now, 1);
with no loop.
Are you sure that is what you want Permf to be assigned?
Any time you have a for loop that is not doing reduction (e.g., totaling the P values) and has more nesting levels than the number of indices of the output variable, then you should be optimizing to do only what would be left in the final result after the overly-nested assignments were complete.
Best Answer