How do I find the element address in an array involved in a loop by applying conditions?
Afficher commentaires plus anciens
I am deriving solution for finite difference method. I have to determine at what value of n does value of T(151) reaches 25. How do I do that?
here m = 200.
Updated syntax:
t=0;
dt = 0.0056;
m = 2000;
for n = 1:15000
T(1) = 30;
t = t + dt;
T0 = T;
for i = 2:m
T(i) = T0(i) - C*T0(i+1) + C*T0(i-1) + D*T0(i+1) - D*2*T0(i) + D*T0(i-1);
end
T(m+1) = T(m);
ifT(151) == 25
fprintf('Value is %d',n);
end
end
The temperature at i=151 is 25 by analytical solution but the program does not return any value of n.
Please help
Réponse acceptée
Plus de réponses (1)
KALYAN ACHARJYA
le 7 Mai 2019
Modifié(e) : KALYAN ACHARJYA
le 7 Mai 2019
As your code seems mess to me, althoght I have given the answer you asked for.
when T array, Tth 125 element equal to 25, if condition true, so it print n values at present exucation.
if T(125)==25
fprintf('Value is n is %d',n);
end
Suggestion to avoid multiples loops for efficient code (If possible).
5 commentaires
madhan ravi
le 7 Mai 2019
Modifié(e) : madhan ravi
le 7 Mai 2019
“Suggestion to avoid multiples loops for efficient code.”
And the alternative would be?
Anush Karki
le 7 Mai 2019
Guillaume
le 7 Mai 2019
Well, certainly the inner i loop is unneeded
Anush Karki
le 7 Mai 2019
KALYAN ACHARJYA
le 7 Mai 2019
Modifié(e) : KALYAN ACHARJYA
le 7 Mai 2019
@Anush There are multiple thread in this forum and public domain, how to avoid loops (If possible), please go through. Link 1 Link 2 or logical indexing do Google search.
That was my suggestion only.Your actual question was
How do I find the element address in an array involved in a loop by applying conditions?
Here T is a array, you can check during exucation, whenther T(125)=25 or not,
As you have tried in your code, its works
if T(125)==25
fprintf('Value is n is %d',n);
end
Anotherway
If you have T array, want to know wheather T(125) = 25 or not.
condition=find(T(125)==25);
if true, condition return 1 value, then only print the n value.
Or
After calculation, you can find the index position, where elements value is 25, as mention below-
>> a=[10 21 33 40 50 50];
>> idx=find(a(:)==50)
idx =
5
6
Catégories
En savoir plus sur Code Performance 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!