Save value from overwriting in while loop

end
In this while loop I want to save u(end) and u1(end) in a vector but when i Try to do it it becomes problematic. Firslty I know how to do it if it only would been one variable (u), but in m case it is u1 too. For every iteration u1 and u ( last element of those ) gets overwritten so I dont know how to save u and u1 ( last elements) in a vector.

2 commentaires

Ameer Hamza
Ameer Hamza le 8 Mai 2020
Mohammed Hassan, This forum is for public benefit. Deleting the code in question makes the question meaningless for anyone else. I am restoring the code so that anyone else coming to this form can have a better idea about the original question.
Text of original Question:
hI
En=50
While d >tollerance
[g u] = main(En,k)
En=En*3;
[g u1] = main(En,k)
d=abs((u(En/3)+1)-u1(En+1));
end
In this while loop I want to save u(end) and u1(end) in a vector but when i Try to do it it becomes problematic. Firslty I know how to do it if it only would been one variable (u), but in m case it is u1 too. For every iteration u1 and u ( last element of those ) gets overwritten so I dont know how to save u and u1 ( last elements) in a vector.

Connectez-vous pour commenter.

Réponses (1)

Ameer Hamza
Ameer Hamza le 7 Mai 2020
Modifié(e) : Ameer Hamza le 7 Mai 2020
The most simplest way is to let the array grow dynamically
En=50
u = [];
u1 = [];
while d >tollerance
[g u(end+1)] = main(En,k)
En=En*3;
[g u1(end+1)] = main(En,k)
d=abs((u(En/3)+1)-u1(En+1));
end

13 commentaires

mohamed hassan
mohamed hassan le 7 Mai 2020
Also, I want the values to be in the same vector where the first value will be u and the second u1 and the third u and the fourth u1 so and so on.
Ameer Hamza
Ameer Hamza le 7 Mai 2020
Are the outputs of main(En,k) scalar or a vector?
mohamed hassan
mohamed hassan le 7 Mai 2020
oh when the mainfiles runs I get a vector with many values of g and the same for u. But I am only intrested in the last value of u so I want to store it in a new vector where I have the last element for u and u1.
Ok, you can do something like this.
En=50
u = [];
while d >tollerance
[g temp] = main(En,k)
u(end+1) = temp(end);
En=En*3;
[g temp] = main(En,k)
u(end+1) = temp(end);
d=abs((u(En/3)+1)-u1(En+1));
end
Note that it create vector u such that odd elements u(1), u(3), u(5) contain value from first main() and even elelemts contain values from second main()
mohamed hassan
mohamed hassan le 7 Mai 2020
So you ignored u1 ?
mohamed hassan
mohamed hassan le 7 Mai 2020
I think something is mising from your ? you just replicated the lines ? and do I need to define temp before ? or do I need to disp u in the end ?
Ameer Hamza
Ameer Hamza le 7 Mai 2020
You mentioned before "Also, I want the values to be in the same vector where the first value will be u and the second u1 and the third u and the fourth u1 so and so on". So my code create one vector u and save all elements in it.
"do I need to define temp before". No. temp is output variable. You don't need to define it before.
"do I need to disp u in the end". yes, If you want to see the value of u in the command window, then use disp(u)
Ameer Hamza
Ameer Hamza le 7 Mai 2020
Modifié(e) : Ameer Hamza le 7 Mai 2020
Oh! I missed that u1. Are you trying to subtract the value of u and u1 (from your code) in each loop. If yes, then try this
En=50
u = [];
while d >tollerance
[g temp1] = main(En,k)
u(end+1) = temp1(end);
En=En*3;
[g temp2] = main(En,k)
u(end+1) = temp2(end);
d=abs(temp1-temp2);
end
mohamed hassan
mohamed hassan le 7 Mai 2020
and in you latest code, ou save the vector from main as temp 1 and store it in the last element in u ? is that correct ?
Ameer Hamza
Ameer Hamza le 7 Mai 2020
I missed the to assign last elements of temp1 and temp2. Now try the updated code in my last comment. I think it should do what you want.
mohamed hassan
mohamed hassan le 7 Mai 2020
Thanks It worked! but I dont actually understand the line u(end+1) = temp1(end);. could you explain it ?
Ameer Hamza
Ameer Hamza le 7 Mai 2020
'temp1(end)' means that the last element of the vector temp1. Since you mentioned that main() returns a vector, and you want to keep the last value of that vector. The 'temp1(end)' keeps that last value.
u(end+1) tells the MATLAB to extend the array 'u' and add the new element to the extended location. This makes sure that the last value is not overwritten.
mohamed hassan
mohamed hassan le 7 Mai 2020
Thank you , I just wondering if it's possible to make it more adaptive ? more efficient, because I use the same kinda code twice in the loop?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by