Comparing data from previous iteration with data from this iteration

4 views (last 30 days)
I want to terminate my loop when the difference between two iteration has an L2 norm of less than 0.1% I thought this was done in the following way;
Diff = 0.5; Data(0) = 0; While Diff >0.1 Adashpad = padarray(Adash,[1 1 1]);
Data(i) = Adash - sigma.*NORMALISED;
Diffpart1 = (Data(i) - Data(i-1));
Diffa = Diffpart1(:);
part1 = sqrt(sum(abs(Diffa)).^2);
Adashpart2 = Adash(:);
part2 = sqrt(sum(abs(Adashpart2)).^2);
Diff1 = (part1./part2)*100;
Adash = Data;
end
However, this throws up the following error;
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in ==> reconstruction_of_image3 at 266 Data(i) = Adash - sigma.*NORMALISED;
Also can I just check that by assigning Adash = Data at the end of my while loop it will cause Data to be used in place of Adash at the top of my loop and so it will go through the processes again. I have not used many while loops so was a bit confused on how to get them to repeat.
Many thanks for any help you can give

Answers (2)

Iain
Iain on 17 Jun 2013
Adash = Data; copies the WHOLE of data into Adash. When you then start the loop at the top again, and "Data(i) = Adash - s...", tells matlab to then put the whole of the old "Data" into a single element of data.
while Diff > 0.1
old = current;
current = ... some recalculation
Diff = (old / current) * 100;
end

Bran
Bran on 17 Jun 2013
Hi there,
yes I see the problem with what I was trying to do. I have only used while loops in the context of single value solutions before and so data(i) could be used.
I have input your solution however, I get the error that 'current' is undefinied. Should I preset it to like 0 outside of the while loop? However, this may cause a problem because I am setting it to a single value and not a 3D array. How can I get around this?

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!