storing variables data after certain iterations

1 view (last 30 days)
iam optimizing ceratain number of varible once i started running optimization code for each ieration values of varibles changes,
after certain iterations i want to store that value into some x1,x2....xn.. how to write code for that can any one help..
ex:
iteration 1: Best Cost = 0.010304
Best Position = 9.935771434 5.937844081 10.26462556 6.847216322 9.001531459 7.667624042 8.710298839 5.69971268 262197.9678 52276.48288 241450.9351 20179.28819 31593.98211 43639.22295 97615.98345 81013.38274 153653.525 26735.13785 276671.0846 179259.3458 290113.7686 212344.5617 133422.8855 1772.70142 1864.244515 1485.612646 1590.65537 3307.513039 959.5461412 1276.736015 1493.995928 1250.033254 3055.218994 3382.962077 3694.866234 1233.594533 3279.514528 1406.979881
Iteration 2: Best Cost = 0.006183
Best Position = 8.568976425 6.569084892 10.64394061 7.799890076 8.640757844 5.93734805 7.817889263 5.778834443 67429.67437 228382.5541 147189.2898 156128.1603 47111.56509 69985.72208 79394.70255 254363.783 218661.6092 23403.03387 166218.996 243277.7029 123297.1135 85742.57053 184053.897 1979.407464 1663.717365 3425.286486 1872.842912 872.050482 2704.546292 970.4243658 1251.680915 2301.327541 3368.827192 1431.257717 1007.175253 3650.449989 2994.557233 1036.824149
in above example for iteration1 i want to store all above values into x1,x2......x38.. how to store that
  5 Comments
Stephen23
Stephen23 on 26 Nov 2019
@veeresh g : what is the point of defining loops that only iterate once? Your loops do not have matching end statements, so your code is incomplete and will not run. If you want the loops to iterate multiple times and you want to store the result values in an array then you will need to use indexing. Learn about indexing within loops:
or search this forum.
veeresh g
veeresh g on 26 Nov 2019
i need to caluculate some values by using each row values for that i vary each time "i" values

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 23 Nov 2019
Edited: Adam Danz on 24 Nov 2019
Almost no one in this forum will support the idea of creating variables to store each value of your array. That would require dynamic variable naming which is to be avoid at all cost. Instead, use indexing as Stephen Cobeldick suggested above. Here's an example.
BestPosition = [
9.935771434 5.937844081 10.26462556 6.847216322
9.001531459 7.667624042 8.710298839 5.69971268 262197.9678
52276.48288 241450.9351 20179.28819 31593.98211 43639.22295
97615.98345 81013.38274 153653.525 26735.13785 276671.0846
179259.3458 290113.7686 212344.5617 133422.8855 1772.70142
1864.244515 1485.612646 1590.65537 3307.513039 959.5461412
1276.736015 1493.995928 1250.033254 3055.218994 3382.962077
3694.866234 1233.594533 3279.514528 1406.979881];
% instead of x4
BestPosition(4)
% instead of x12
BestPosition(12)
%instead of xn
BestPosition(n)
  2 Comments
Stephen23
Stephen23 on 23 Nov 2019
Edited: Stephen23 on 23 Nov 2019
"Almost no one in this forum will support the idea of creating variables..."
Nor does the MATLAB documentation: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."
veeresh g
veeresh g on 26 Nov 2019
Edited: veeresh g on 26 Nov 2019
thanq Stephen sir.. actually i stored all varibles in text file and read the fie with dlmread command as below
% 50_6 is text file name
d=dlmread('50_6.txt');
for i1=1 % to retrive first row
for j1=1
m1=d(i1,j1);m2=d(i1,j1+1);m3=d(i1,j1+2); m4=d(i1,j1+3);

Sign in to comment.

Categories

Find more on Variables 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!