how to run function 1000 times to find minimum

조회 수: 1 (최근 30일)
Kangkeon Jeon
Kangkeon Jeon 2019년 10월 14일
댓글: Rena Berman 2019년 10월 28일
i have a main function that eventually gives a score with randomized grouping of names. score is just one number calculated through function. I want to run this 1000 times to have 1000 scores and want to know minimum and know which grouping gave that score.
how can i do this?
  댓글 수: 3
Stephen23
Stephen23 2019년 10월 16일
Original question (from Google Cache):
i have a main function that eventually gives a score with randomized grouping of names. score is just one number calculated through function. I want to run this 1000 times to have 1000 scores and want to know minimum and know which grouping gave that score.
how can i do this?
Rena Berman
Rena Berman 2019년 10월 28일
(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

채택된 답변

per isakson
per isakson 2019년 10월 14일
편집: per isakson 2019년 10월 14일
Something like
S = struct( 'score', cell(1,1000), 'grouping', cell(1,1000) );
for jj = 1 :1000
S(jj).grouping = randomized_grouping_of_names();
S(jj).score = main_function( S(jj).grouping );
end
[~,ix_min] = min([S.score]);
S(ix_min)
In response to comment
What's in the file, FileName ?
FileName = ???
groupsize = ???
data = readtable( FileName, 'ReadRowNames', true );
number_of_people = size(data, 2);
number_of_groups = ceil( number_of_people / groupsize );
S = struct( 'score', cell(1,N), 'grouping', cell(1,N) );
for jj = 1 : N
S(jj).grouping = generate_random_grouping( data, number_of_people, number_of_groups );
S(jj).score = score_grouping( data, S(jj).grouping, ~ );
end
[~,ix_min] = min([S.score]);
S(ix_min)
No good.
With substatial help from Walter
>> subject_scores = Jeon_clust( 'Biol_528_2019_sheet.xlsx', 3 )
subject_scores =
struct with fields:
score: 0.66667
grouping: {1×8 cell}
where
function S = Jeon_clust( FileName, groupsize )
N = 1000;
data = readtable( FileName, 'ReadRowNames', true );
number_of_people = size(data, 2);
number_of_groups = ceil( number_of_people / groupsize );
S = struct( 'score', cell(1,N), 'grouping', cell(1,N) );
for jj = 1 : N
S(jj).grouping = generate_random_grouping( data, number_of_people, number_of_groups );
S(jj).score = sum_total_subject_score( data, S(jj).grouping, number_of_groups );
end
[~,ix_min] = min([S.score]);
S = S(ix_min);
end
and
function stss = sum_total_subject_score( data, grouping, number_of_groups )
subject_scores = score_grouping(data, grouping,number_of_groups);
group_ = [subject_scores{1:number_of_groups}];
group_scores = group_.';
diff_score = diff(group_scores);
total_subject_score = abs(sum(diff_score));
stss = sum(total_subject_score);
end
  댓글 수: 5
per isakson
per isakson 2019년 10월 14일
What is the size of the table data ?
Kangkeon Jeon
Kangkeon Jeon 2019년 10월 14일
편집: Kangkeon Jeon 2019년 10월 16일
i attached the file!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2019년 10월 14일
N = 1000;
all_scores = zeros(1,N);
all_groupings = cell(1,N);
for iter = 1 : N
[all_scores(iter), all_groupings{N}] = generate_one_grouping_and_score_it();
end
[minscore, idx] = min(all_scores);
grouping_for_minscore = all_groupings{idx};
disp(minscore)
disp(grouping_for_minscore)
  댓글 수: 4
Kangkeon Jeon
Kangkeon Jeon 2019년 10월 14일
편집: Kangkeon Jeon 2019년 10월 16일
i tried this but it gave "Insufficient number of outputs from right hand side of equal sign to satisfy assignment" error. am i doing this wrong?
Walter Roberson
Walter Roberson 2019년 10월 14일
function [best_score, best_grouping] = Jeon_clust(FileName, groupsize)
data = readtable(FileName, 'ReadRowNames', true);
number_of_people = size(data, 2);
number_of_groups = ceil(number_of_people / groupsize);
number_of_questions = size(data, 1);
N = 1000;
all_scores = zeros(1,N);
all_groups = cell(1,N);
for iter = 1 : N
grouping = generate_random_grouping(data, number_of_people, number_of_groups);
subject_scores = score_grouping(data, grouping,number_of_groups);
group_ = [subject_scores{1:number_of_groups}];
group_scores = group_.';
diff_score = diff(group_scores);
total_subject_score = abs(sum(diff_score));
score = sum(total_subject_score);
all_scores(iter) = score;
all_groups{iter} = grouping;
end
[best_score, idx] = min(all_scores);
best_grouping = all_groups{idx};

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Photonics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by