how to save values in matlab after iteration
2 views (last 30 days)
Show older comments
Kareem AlBokhari
on 22 Sep 2015
Commented: Walter Roberson
on 22 Sep 2015
I have 2 for loops
for c=1200:100:1600
for b=5:5:20
I want it to save the value in an array after finishing from 5 till 20 ex takes 1200 the iterates from 5 till 20 in specific equation before going back to c it saves the values in an array , then when taking 1300 also saves the values from 5 till 20 in a seperate array
0 Comments
Accepted Answer
Walter Roberson
on 22 Sep 2015
cvals = 1200:100:1600;
bvals = 5:5:20;
for cidx = 1 : length(cvals)
c = cvals(cidx);
for bidx = 1 : length(bvals)
b = bvals(bidx);
.... compute something
result{cidx}(bidx) = .... some scalar
end
end
4 Comments
Walter Roberson
on 22 Sep 2015
If you are not using the value of c then you have not used those temperatures in your code.
I recommend that you switch to 2-dimensional arrays:
cvals = 1200:100:1600;
bvals = 5:5:20;
for cidx = 1 : length(cvals)
c = cvals(cidx);
for bidx = 1 : length(bvals)
b = bvals(bidx);
.... compute something
result(cidx,bidx) = .... some scalar
end
end
Then you will have one row for each c value, which can be accessed as (for example)
result(3,:)
And printed out...
fprintf('for c = %f, b = %f %f %f %f\n', [cvals(:), result].')
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays 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!