How to find the index value of an element in a cell array?
5 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Adam Drake
2023-3-13
编辑:Adam Drake
2023-3-13
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
fclose(fileSTATS);
17 个评论
Adam Drake
2023-3-14
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
fclose(fileSTATS);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!