How to find the index value of an element in a cell array?

Hello,
I have tried by all means finding an example for my case but I only got frustration, so I hope you can help me.
Context: I am using a Differential Evolution Algorithm and as a part of the results I calculate the mean, sd and also the best OF value (in this case, the minimum value) of each generation (iteration). I run the algorithm 6 times and I obtain 6 data.txt files (runde*.txt) containing the following information: iteration, OF value, parameters values (2 params.) and the run number (i.e., if it is the 1st, 2nd, 3rd etc run). Each file contains 5 columns (named before) and 50 rows. This is the code I am using:
files = dir('runde*.txt');
n=length(files);
for i = 1:n
filename = sprintf('runde%d.txt', i);
dedata{i} = importdata(filename);
end
m=length(dedata{i}.data);
fileSTATS=fopen('destats.txt', 'w');
MOF=[]; % matrix in which columns contain best OF values (S_bestval.FVr_oa)...
...per generation of every run
for j=1:m
for i=1:n
MOF(j,i)=dedata{1,i}.data(j,2);
mean_MOF(j,1)=mean(MOF(j,:));
sd_MOF(j,1)=std(MOF(j,:));
best_MOF(j,1)=min(MOF(j,:));
end
fprintf(1,'Gen.: %d,\t mean: %.6e,\t SD: %.6e,\t best: %.6e\n',j,mean_MOF(j),sd_MOF(j),best_MOF(j));
fprintf(fileSTATS,'%d\t %e\t %e\t %e\r\n',j,mean_MOF(j),sd_MOF(j),best_MOF(j));
end
fclose(fileSTATS);
MOF stores the OF values of every file (which are in column 2).
My goal is to store (besides the data indicated in the code) in the file destats.txt the data (OF parameters and run number) associated to each value of the vector best_MOF (a vector which collects the best OF value of every generation off all runs of the algorithm).
I suspect it is a question of finding the position of every best_MOF(j,1) and indicating the data to store according to the index of every element best_MOF(j,1). That is the reason for the question. I don't know if it the correct question for this problem. I hope as well I have explained my problem clearly.
I include files in case they are needed. Note (Ed.): As I explained above, the goal is to write next to best_MOF column in destats.txt file the corresponding values in runde*.txt files. Note that the array best_MOF(j,1) does not operate values, it only selects the minimun value of the 2nd column of all six runde*.txt files row by row. Therefore, the 5th, 6th and 7th columns of destats.txt will be the so-named corresponding values of 3rd, 4th and 5th columns of runde*.txt files.
I truly appreciate any advise or solution for this problem. I have little experience with programming.
Thank you very much.

 Accepted Answer

Had to do quite a bit of imagining without the starting data files, but I think this will set you straight. Let me know if you have questions or if it doesn't work.
EDIT: fixed case where length of data set in runde*.txt files are different for each file. Console shows dataset length but it is not ouput to file. Easily added if needed. Add attached "runde7.txt" to directory and run to see functionality. runde7 has 52 rows instead of 50. Note that MOF will contain "NaN" on files with datasets less than max(len). Account for NaN's with 'omitnan' flag on mean, std, and min functions.
EDIT2: Added best parameters to console and file output
EDIT3: Made per run instead of per file.
clc, clear all
files = dir('runde*.txt');
n = length(files);
for i = 1:n
filename = sprintf('runde%d.txt', i);
dedata{i} = importdata(filename);
len(i) = length(dedata{i}.data);
end
m = max(len);
MOF = nan(n,m); % matrix in which columns contain best OF values (S_bestval.FVr_oa)...
...per generation of every run
for i = 1:n % number of files
for j = 1:len(i) % length of data
MOF(i,j)=dedata{i}.data(j,2); % OF Values stored in column 2 of data
end
end
mean_MOF = mean(MOF,1,'omitnan'); % row vector with mean of each column (run)
sd_MOF = std(MOF,0,1,'omitnan'); % row vector with std of each column (run)
[best_MOF,best_idx] = min(MOF,[],1,'omitnan'); % row vector with min of each column (run)
p1 = zeros(1,m);
p2 = zeros(1,m);
run = zeros(1,m);
fileSTATS=fopen('destats.txt', 'w');
for j = 1:m
p1(j) = dedata{best_idx(j)}.data(j,3);
p2(j) = dedata{best_idx(j)}.data(j,4);
run(j) = dedata{best_idx(j)}.data(j,5);
fprintf(1,'Gen.: %d,\tmean: %.6e,\tSD: %.6e,\tBest: %.6e,\tp1: %.6e\tp2: %.6e\trun: %d\n', ...
j,mean_MOF(j),sd_MOF(j),best_MOF(j),p1(j),p2(j),run(j));
fprintf(fileSTATS,'%d\t %e\t %e\t %e\t %e\t %e\t %d\r\n', ...
j,mean_MOF(j),sd_MOF(j),best_MOF(j),p1(j),p2(j),run(j));
end
Gen.: 1, mean: 1.777127e+00, SD: 7.709961e-01, Best: 6.468260e-01, p1: -5.762191e-02 p2: -1.120718e+00 run: 6 Gen.: 2, mean: 1.391021e+00, SD: 6.838437e-01, Best: 5.943482e-01, p1: 1.074498e+00 p2: -3.054331e+00 run: 6 Gen.: 3, mean: 9.805492e-01, SD: 6.692086e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 4, mean: 9.805492e-01, SD: 6.692086e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 5, mean: 9.089068e-01, SD: 5.807616e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 6, mean: 9.089068e-01, SD: 5.807616e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 7, mean: 7.496810e-01, SD: 5.243635e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 8, mean: 7.496810e-01, SD: 5.243635e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 9, mean: 6.828632e-01, SD: 4.422585e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 10, mean: 5.668665e-01, SD: 4.279609e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 11, mean: 5.668665e-01, SD: 4.279609e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 12, mean: 5.668665e-01, SD: 4.279609e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 13, mean: 4.932501e-01, SD: 4.334705e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 14, mean: 4.347951e-01, SD: 2.915590e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 15, mean: 4.347951e-01, SD: 2.915590e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 16, mean: 4.347951e-01, SD: 2.915590e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 17, mean: 4.347951e-01, SD: 2.915590e-01, Best: 2.453411e-01, p1: -3.071395e+00 p2: -1.000331e+01 run: 2 Gen.: 18, mean: 4.140266e-01, SD: 3.049505e-01, Best: 2.018446e-01, p1: 8.605031e-02 p2: -2.182238e-02 run: 4 Gen.: 19, mean: 3.720139e-01, SD: 3.401474e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 20, mean: 3.701877e-01, SD: 3.400666e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 21, mean: 3.701877e-01, SD: 3.400666e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 22, mean: 3.175751e-01, SD: 2.234774e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 23, mean: 3.175751e-01, SD: 2.234774e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 24, mean: 3.175751e-01, SD: 2.234774e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 25, mean: 3.175751e-01, SD: 2.234774e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 26, mean: 3.175751e-01, SD: 2.234774e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 27, mean: 2.435447e-01, SD: 1.174528e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 28, mean: 2.435447e-01, SD: 1.174528e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 29, mean: 2.435447e-01, SD: 1.174528e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 30, mean: 2.435447e-01, SD: 1.174528e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 31, mean: 2.372693e-01, SD: 1.183402e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 32, mean: 2.344517e-01, SD: 1.158099e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 33, mean: 2.344517e-01, SD: 1.158099e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 34, mean: 2.344517e-01, SD: 1.158099e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 35, mean: 2.237176e-01, SD: 1.222427e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 36, mean: 2.237176e-01, SD: 1.222427e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 37, mean: 2.237176e-01, SD: 1.222427e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 38, mean: 2.211849e-01, SD: 1.201115e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 39, mean: 2.211849e-01, SD: 1.201115e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 40, mean: 2.211849e-01, SD: 1.201115e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 41, mean: 2.211849e-01, SD: 1.201115e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 42, mean: 2.051711e-01, SD: 1.137674e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 43, mean: 2.051711e-01, SD: 1.137674e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 44, mean: 2.051711e-01, SD: 1.137674e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 45, mean: 2.051711e-01, SD: 1.137674e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 46, mean: 2.051711e-01, SD: 1.137674e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 47, mean: 1.963109e-01, SD: 1.105632e-01, Best: 4.179763e-02, p1: 9.760521e-01 p2: -1.021824e+00 run: 1 Gen.: 48, mean: 1.884904e-01, SD: 1.170291e-01, Best: 1.857550e-02, p1: 9.822058e-01 p2: -9.849300e-01 run: 1 Gen.: 49, mean: 1.872204e-01, SD: 1.168202e-01, Best: 1.857550e-02, p1: 9.822058e-01 p2: -9.849300e-01 run: 1 Gen.: 50, mean: 1.872204e-01, SD: 1.168202e-01, Best: 1.857550e-02, p1: 9.822058e-01 p2: -9.849300e-01 run: 1
fclose(fileSTATS);

17 Comments

Dear Adam,
Thank you for your response. Here you can find attached the files. I hope you can use them to check your answer and edit it if necessary. I will wait for you to test your code with the files.
Thank you again
Seems to work perfectly. Results are:
console
Gen.: 1, mean: 3.379283e-01, SD: 5.187342e-01, best: 1.857550e-02
Gen.: 2, mean: 2.695424e-01, SD: 1.998604e-01, best: 2.000687e-01
Gen.: 3, mean: 8.355956e-01, SD: 5.606049e-01, best: 3.792330e-01
Gen.: 4, mean: 4.188311e-01, SD: 4.805082e-01, best: 1.374399e-01
Gen.: 5, mean: 3.619961e-01, SD: 3.505464e-01, best: 2.037424e-01
Gen.: 6, mean: 3.479943e-01, SD: 1.178959e-01, best: 1.842626e-01
destats.txt
1 3.379283e-01 5.187342e-01 1.857550e-02
2 2.695424e-01 1.998604e-01 2.000687e-01
3 8.355956e-01 5.606049e-01 3.792330e-01
4 4.188311e-01 4.805082e-01 1.374399e-01
5 3.619961e-01 3.505464e-01 2.037424e-01
6 3.479943e-01 1.178959e-01 1.842626e-01
Thank you Adam.
I think I did not explained well. Actually, that is what my code was plotting in the destats.txt file. I have edited question for more clarity (I hope so).
The case is that I need, besides that four data (Gen., mean, SD, and best OF), to add 3 columns more, corresponding to the values of 3rd, 4th and 5th columns in runde* files (that is, parameter 1, parameter 2 and run number). The problem is that those values must correspond with the best_MOF values.
For example, 1st element of best_MOF array correponds with the 1st row of the runde6.txt (that is, the element dedata{1,6}.data(1,2) of the cell dedata). Therefore, the elements dedata{1,6}.data(1,3), dedata{1,6}.data(1,4) and dedata{1,6}.data(1,5) of the cell array dedata should be added to the first row of the destats.txt file.
And so on...
I hope it is more clear now.
See edit for file length correction.
See edit for best parameters addition.
I am sorry Adam, but that is not what I am looking for... maybe I wrote the previous answer after you make the edit. Please, check it out. I think it will give you a better idea of what I need.
I any case, fprintf line for the file should contain 7 variables. Something like this:
fprintf(fileSTATS,'%d\t %e\t %e\t %e\t %e\t %e\t %d\r\n',j,mean_MOF(j),sd_MOF(j),best_MOF(j),parameter1,parameter2,Run);
parameter1, parameter2 and Run are the suposed variables to introduce in that line such that destats.txt file contains the data in the way I explained in the previous answer.
This is how the 6 first rows of the destats.txt file should show (according to the data provided):
1 1.777127e+00 7.709961e-01 6.468260e-01 -5.762191e-02 -1.120718e+00 6
2 1.391021e+00 6.838437e-01 5.943482e-01 1.074498e+00 -3.054331e+00 6
3 9.805492e-01 6.692086e-01 2.453411e-01 -3.071395e+00 -1.000331e+01 2
4 9.805492e-01 6.692086e-01 2.453411e-01 -3.071395e+00 -1.000331e+01 2
5 9.089068e-01 5.807616e-01 2.453411e-01 -3.071395e+00 -1.000331e+01 2
6 9.089068e-01 5.807616e-01 2.453411e-01 -3.071395e+00 -1.000331e+01 2
I hope this gives you a better perpective.
I think you need to check your numbers. try, adding "clc, clear all" to the top of your script to make sure you're running from scratch.
To clarify:
column 2 of data for each file is stored in MOF.
The mean of column 2 of data for each file is stored in mean_MOF
The std of column 2 of data for each file is stored in sd_MOF
The minumum value of column 2 of data for each file is stored in best_MOF
best_param contains column 3,4,5 of data for each file at the index where the minimum value of column 2 was found.
best_idx: the index at the minimum value for files 1-6 are
48
49
27
35
42
48
I see now. You don't need the data per file, you need it across files. Working on it.
I ran the algorith again and it uses random numbers so they have changed. However the program provided only needs the files I provided, so if that data does not change, the destats file should not vary.
Anyway, after running your code I only got 6 rows, not 50, and by some unknown reason the gen number is the same than the run number. I don't know if that is what you got too.
If you use the data I provided the answer for the first 6 rows (of 50) should be that I wrote before.
Thank you again
fixed. Because you were showing 6 rows, I figured the stats were per file instead of per run across all files. Should be what you want now.
Thank you so much Adam. Indeed now it seems now it works.
However my intention was to code it as generic as possible, because in future I will use more parameters. Your code gave me some idea anyway.
If you don't mind, I would like you to explain why you did some steps. It is not my intention to question your solution, but to understand (and learn) why things are working in that way.
1. Why do you calculate the length for every element of the cell (I mean the six elements of the cell)?
len(i) = length(dedata{i}.data);
I don't understand either why you use m=max(len).
2. Which is the purpose of using omitnan in these lines?
mean_MOF = mean(MOF,1,'omitnan'); % row vector with mean of each column (run)
sd_MOF = std(MOF,0,1,'omitnan'); % row vector with std of each column (run)
[best_MOF,best_idx] = min(MOF,[],1,'omitnan'); % row vector with min of each column (run)
I don't know exactly the purpose of the vector of the third line.
3. What is nan(n,m) for? is it like zeros(n,m)?
I guess I will have to look for a way to add n parameters to the file (considering run number a parameter as well) from the 3rd column of the dedata{i}.data to the nth column (since parameters in the runde files are stored from the 3rd column onwards). To do that it is important considering have information of the indexes of best_MOF data in the dedata{i}.data cell, and that is (I think) what you get in this line:
[best_MOF,best_idx] = min(MOF,[],1,'omitnan');
I will try to adapt your answer. Any advise will be wellcome.
Thank you so much for your help.
Are you scrolling to the right to see the full output? Because I have added the two parameters and run number (columns 3, 4, and 5) to the console output and file output and I'm getting the numbers you were looking for.
If you wanted to have all columns from column 3 on output to one variable you would use:
endColumn = size(dedata{j}.data,2); remainingColumns(j,:) = dedata{best_idx(j)}.data(j,3:endColumn)
Questions 1 and 2 and 3 go together. I was accounting for the possibility that you may have a different number of generations for each run (i.e. one may have 50, another may have 52). In your initial script they would have only read whatever the size of the last file was. NaN(n,m) preallocates an array that is the number of files by maximum number of data points in any file. If you have 17 files and all of your files have 50 generations then it will be 17x50 and no cell will have NaN. If one of those files has 52 generations then it will be 17x52 and every row except 1 will have 2 columns of NaNs (not a number). The omitnan flag will ignore the NaNs when computing mean, std, and min. If I had preallocated with zeros then those calculations would be incorrect since you'd be adding 2 zeros to each dataset except the one with 52 generations. It is probably the case that all of your files will have the same number of generations but this will account for them either way.
For the last question, functions like min and max can return the index of the value it finds. So [best_MOF,best_idx] = min(MOF,[],1,'omitnan'); find the minimum value across each generation and doesn't consider NaNs and returns the minimum value and the index that it was found. Since MOF is file number × generation number in size and the min function specifies the operation is done per column, what is returned is a row vector that is 1×50 where each cell is the minimum for that generation along with the index, or file number, where that value was found.
Dear Adam,
Now it seems the main problem is solved, but there is still a little issue.
This is the code I have now:
files = dir('runde*.txt');
n=length(files);
for i = 1:n
filename = sprintf('runde%d.txt', i);
dedata{i} = importdata(filename);
end
m=length(dedata{i}.data);
fileSTATS=fopen('destats.txt', 'w');
MOF=[]; % matrix in which columns contain best OF values (S_bestval.FVr_oa)...
...per generation of every run
for j=1:m
for i=1:n
MOF(j,i)=dedata{1,i}.data(j,2);
mean_MOF(j,1)=mean(MOF(j,:));
sd_MOF(j,1)=std(MOF(j,:));
best_MOF(j,1)=min(MOF(j,:));
[best_MOF,best_index]=min(MOF,[],2);
best_params(j,:)=dedata{best_index(j)}.data(j,3:(end-1));
best_run(j,1)=dedata{best_index(j)}.data(j,end);
end
fprintf(1,'Gen.: %d,\t mean: %.6e,\t SD: %.6e,\t best: %.6e,\t run: %d \n',...
j,mean_MOF(j),sd_MOF(j),best_MOF(j),best_run(j));
fprintf(fileSTATS,'%d\t %e\t %e\t %e\t',j,mean_MOF(j),sd_MOF(j),best_MOF(j));
for k=1:min(size(best_params))
fprintf(1,'params.(%d): %.6e \n',k,best_params(j,k));
fprintf(fileSTATS,'%e\t',best_params(j,k));
end
fprintf(fileSTATS,'%d\r\n',best_run(j));
end
fclose(fileSTATS);
It is not exactly what you posted above but the idea is the same (I think). The key was the use of the min() function. I didn't know to use it that way. One redundant fact is that the number of runs will be always (at least in my algortihm) the same than the number of the cell elements. I also avoided the use of NaNs since the structure of the runde* files will be always the same.
Well, the problem now is that in the first row in both console and file destats.txt only shows one parameter. I don't know why. I have tried many modifications but the result is always the same. As you may check, the problem is related to the print part, since in the workspace best_params shows 2 columns.
Can you give me some hint of what the problem is?
Thank you one more time.
for k=1:min(size(best_params))
You do not want that. You are you using k to index columns, so you want k = 1 : size(best_params,2)
yes! it worked!
So much to learn yet... Thank you so much!
By the way, why it didn't work using min()?
You're trying to do mean and std and min before you've fully populated MOF. you have to finish that loop first. Please accept my answer.
clc, clear all
files = dir('runde*.txt');
n = length(files);
for i = 1:n
filename = sprintf('runde%d.txt', i);
dedata{i} = importdata(filename);
len(i) = length(dedata{i}.data);
end
m = length(dedata{i}.data);
MOF = []; % matrix in which columns contain best OF values (S_bestval.FVr_oa)...
...per generation of every run
% YOU HAVE TO ALLOCATE MOF WITH VALUES BEFORE YOU CAN TAKE THE MEAN,STD,MIN
for i = 1:n % number of files
for j = 1:len(i) % length of data
MOF(i,j)=dedata{i}.data(j,2); % OF Values stored in column 2 of data
end
end
mean_MOF = mean(MOF); % row vector with mean of each column (run)
sd_MOF = std(MOF); % row vector with std of each column (run)
[best_MOF,best_idx] = min(MOF); % row vector with min of each column (run)
fileSTATS=fopen('destats.txt', 'w');
for j = 1:m
best_params(j,:) = dedata{best_idx(j)}.data(j,3:end-1);
best_run(j) = dedata{best_idx(j)}.data(j,end);
fprintf(1,'Gen.: %d,\tmean: %.6e,\tSD: %.6e,\tBest: %.6e,\t',...
j,mean_MOF(j),sd_MOF(j),best_MOF(j));
fprintf(1,'Parameters:\t');
for k = 1:size(best_params,2) % 2 specifies number of columns
fprintf(1,'%.6e\t',best_params(j,k));
end
fprintf(1,'Run:\t%d\r\n',best_run(j))
fprintf(fileSTATS,'%d\t %e\t %e\t %e\t', ...
j,mean_MOF(j),sd_MOF(j),best_MOF(j));
for k = 1:size(best_params,2) % 2 specifies number of columns
fprintf(fileSTATS,'%e\t',best_params(j,k));
end
fprintf(fileSTATS,'%d\r\n',best_run(j));
end
Gen.: 1, mean: 1.777127e+00, SD: 7.709961e-01, Best: 6.468260e-01,
Parameters:
-5.762191e-02 -1.120718e+00
Run: 6
Gen.: 2, mean: 1.391021e+00, SD: 6.838437e-01, Best: 5.943482e-01,
Parameters:
1.074498e+00 -3.054331e+00
Run: 6
Gen.: 3, mean: 9.805492e-01, SD: 6.692086e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 4, mean: 9.805492e-01, SD: 6.692086e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 5, mean: 9.089068e-01, SD: 5.807616e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 6, mean: 9.089068e-01, SD: 5.807616e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 7, mean: 7.496810e-01, SD: 5.243635e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 8, mean: 7.496810e-01, SD: 5.243635e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 9, mean: 6.828632e-01, SD: 4.422585e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 10, mean: 5.668665e-01, SD: 4.279609e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 11, mean: 5.668665e-01, SD: 4.279609e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 12, mean: 5.668665e-01, SD: 4.279609e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 13, mean: 4.932501e-01, SD: 4.334705e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 14, mean: 4.347951e-01, SD: 2.915590e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 15, mean: 4.347951e-01, SD: 2.915590e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 16, mean: 4.347951e-01, SD: 2.915590e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 17, mean: 4.347951e-01, SD: 2.915590e-01, Best: 2.453411e-01,
Parameters:
-3.071395e+00 -1.000331e+01
Run: 2
Gen.: 18, mean: 4.140266e-01, SD: 3.049505e-01, Best: 2.018446e-01,
Parameters:
8.605031e-02 -2.182238e-02
Run: 4
Gen.: 19, mean: 3.720139e-01, SD: 3.401474e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 20, mean: 3.701877e-01, SD: 3.400666e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 21, mean: 3.701877e-01, SD: 3.400666e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 22, mean: 3.175751e-01, SD: 2.234774e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 23, mean: 3.175751e-01, SD: 2.234774e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 24, mean: 3.175751e-01, SD: 2.234774e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 25, mean: 3.175751e-01, SD: 2.234774e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 26, mean: 3.175751e-01, SD: 2.234774e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 27, mean: 2.435447e-01, SD: 1.174528e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 28, mean: 2.435447e-01, SD: 1.174528e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 29, mean: 2.435447e-01, SD: 1.174528e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 30, mean: 2.435447e-01, SD: 1.174528e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 31, mean: 2.372693e-01, SD: 1.183402e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 32, mean: 2.344517e-01, SD: 1.158099e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 33, mean: 2.344517e-01, SD: 1.158099e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 34, mean: 2.344517e-01, SD: 1.158099e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 35, mean: 2.237176e-01, SD: 1.222427e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 36, mean: 2.237176e-01, SD: 1.222427e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 37, mean: 2.237176e-01, SD: 1.222427e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 38, mean: 2.211849e-01, SD: 1.201115e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 39, mean: 2.211849e-01, SD: 1.201115e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 40, mean: 2.211849e-01, SD: 1.201115e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 41, mean: 2.211849e-01, SD: 1.201115e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 42, mean: 2.051711e-01, SD: 1.137674e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 43, mean: 2.051711e-01, SD: 1.137674e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 44, mean: 2.051711e-01, SD: 1.137674e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 45, mean: 2.051711e-01, SD: 1.137674e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 46, mean: 2.051711e-01, SD: 1.137674e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 47, mean: 1.963109e-01, SD: 1.105632e-01, Best: 4.179763e-02,
Parameters:
9.760521e-01 -1.021824e+00
Run: 1
Gen.: 48, mean: 1.884904e-01, SD: 1.170291e-01, Best: 1.857550e-02,
Parameters:
9.822058e-01 -9.849300e-01
Run: 1
Gen.: 49, mean: 1.872204e-01, SD: 1.168202e-01, Best: 1.857550e-02,
Parameters:
9.822058e-01 -9.849300e-01
Run: 1
Gen.: 50, mean: 1.872204e-01, SD: 1.168202e-01, Best: 1.857550e-02,
Parameters:
9.822058e-01 -9.849300e-01
Run: 1
fclose(fileSTATS);
Thank you Adam for all your help.
I understand what you mean, but note that the process is taking place row by row:
  1. Fill the jth row of MOF
  2. Calculate mean of jth row of MOF
  3. Calculate SD of jth row of MOF
  4. Fill jth row of [best_MOF,best_index]
  5. Fill the jth row of best_params (accordin to best_index(j))
  6. Fill the jth row of best_run (accordin to best_index(j))
And so on, so it is not necessary to fully fill MOF before doing the rest.
Finally the little issue had simple solution.
I have accepted your answer since you have provided me the key to solve this problem.
Thank you.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!