how to save values in matlab after iteration

2 views (last 30 days)
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

Accepted Answer

Walter Roberson
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
Kareem AlBokhari
Kareem AlBokhari on 22 Sep 2015
I could use 1 to 5 but I used 1200 till 1600 bcz I must show the tempratures i have taken in my code ,
Thanks man alooot , the code worked
But last questions , is their a way i could understand ur code and how it worked cz I have to make presentation to present
Walter Roberson
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].')

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!