How do I get values of for loop into a vector?
Show older comments
Hello everyone i have two questions
1) If i want to get my values from my for loop into a vector how do i do this without getting a large vector of zeros?
2) If i wanted to do a nested for loop to have r change as well, how could i do this?
Here is my code:
for r = .175
Bi=(h*r)/k; %Biot Number equation
l=1.0904; %Lambda value for now
A1=1.1548; %A1 value for now
L=.175;
for t=[300,600,900,1200,1800,2400,3000,3600,5400];
L=.175;
T1= (Ti-Tinf)*A1*exp((-(l^2)*((a*t)/L^2)))+Tinf
end
end
for the inner for loop, when i try to put T1(t) to get a vector i get a large vector of 5400 values (my last value of t), which contains mostly zeros. Also, for my outer loop, i want to different values of r, which would then change the l and A1 values that i plug into my T1 equation. How could i go about doing this?
All help is appreciated. Thank you.
Answers (1)
James Tursa
on 17 Nov 2015
Edited: James Tursa
on 17 Nov 2015
Is there some reason you want or need to use a loop for this? E.g., using vectorized code your inner loop can be reduced to this (T1 will be a vector result):
t=[300,600,900,1200,1800,2400,3000,3600,5400];
T1 = (Ti-Tinf)*A1*exp((-(l^2)*((a*t)/L^2)))+Tinf
Caveat: If other variables are vectors, then other adjustments would need to be made to this assignment (e.g., may need to change some * and / operators to element-wise .* and ./ operators, or use bsxfun, etc.)
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!