问题
y = find(sA);
l = y + sA;
for i=1:10
for j=1
l = l + sA;
end
y = y + length(y);
end
I would like to know how to store the value that is generated for l
, for each iteration, in an array.
When I try do something like l(l) = l + sA; I obtain 'weird' results.
NOTE: PLEASE READ MY COMMENTS POSTED BELOW. THANKS!
回答1:
For a complex loop, usually I do something like this:
results = zeros(expectedLength,1);
ixNextResult = 1;
for ixForLoop1 = 1:10
for ixForLoop2 = 20:30
..
results(ixNextResult) = calculationResult;
ixnextResult = ixNextResult + 1;
end
end
I'm having a hard time understanding what your code is trying to accomplish, so I'm not sure what to change. For example the snippet l(l) = l+sA
does not make a lot of sens to me.
回答2:
Try it like this:
y = find(sA); %This is incredibly strange! What exactly are you trying to achieve with this line?
l = y + sA;
for i=1:10
l = l + sA;
StoredL(i, :) = l;
y = y + length(y); %This line does nothing??? Why is it here?
end
I removed your inner loop as it was doing nothing, for j = 1 will only ever run once so what's the point?
来源:https://stackoverflow.com/questions/12272559/how-to-store-value-generated-from-nested-for-loop-in-an-array-in-matlab