Loops + Updating variables
8 views (last 30 days)
Show older comments
I'm trying to do some iterative calculations by repeatedly calling a function 'calculate_box' which returns a column array of values on each iteration. I calculate the first column array outside of any loops, and store the value of the first item (i.e. (1,1) ).
I then start my loop on the 2nd calculation. From the 2nd calculation onwards, I would like my code to compare the first value in the array ( so (1,2) ) with the previous iteration.
If the value is the same, I would like the value to be discarded, and 'shifty' - an input to the function to be increased by one, and the calculation run again and shifty increased, until such a time as the value returned is different to the previous one.
When this occurs, I would like the value to be stored, 'shifty' to be set to this value plus min_dist, then the process to return to the start of the main loop.
My code is below, I'd be very grateful if anyone could explain my error/how to resolve it. Many thanks in advance!
n_eig = 5;
range = [0,50];
min_dist = 1; %min distance between eigenvalues
Initial_matrix = B;
gcompute = [0.1,0.1,0.1,0.1,0.1]; %initial guess
resultz = zeros(n_eig,n_eig);
resultz(:,1) = calculate_box(P,gcompute,100000,range(1));
shifty = resultz(1,1) + min_dist;
for j = 2:1:n_eig
resultz(:,j) = calculate_box(P,gcompute,100000,shifty);
while resultz(1,j) == resultz(1,j-1)
shifty = shifty + min_dist;
resultz(:,j) = calculate_box(P,gcompute,100000,shifty);
end
shifty = resultz(1,j) + min_dist;
end
2 Comments
Daksh
on 17 Nov 2022
Hi, can you share the calculate_box function and other data variables so it is possible to reproduce this issue on my side? Only then I shall be able to debug and resolve the error you're experiencing. Thanks!
Jan
on 17 Nov 2022
Before we can explain the cause of the error, you have to mention, what you consider as error. You have explained the intention of the code and show a certain part of it. We cannot run it, so there is no chance to guess, what the problem is.
Answers (0)
See Also
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!