Comparing data from previous iteration with data from this iteration

1 次查看(过去 30 天)
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

回答(2 个)

Iain
Iain 2013-6-17
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 2013-6-17
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?

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by