How can I get the min of cell arrays
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Zaid Alyasseri
am 17 Dez. 2016
Kommentiert: Zaid Alyasseri
am 17 Dez. 2016
Hi everyone, I faced a problem with my Matlab code. Actually, I have a cell array which is:
BestGene={}
BestFitness={}
Bestresult={}
for i=1:5
Bestresult{i}={BestFitness{i},BestGene{i}}
end
My question is how can I get the BestGene{:} parameters of the min(BestFitness{:}).
for example:
BesrFitness{1}=0.022;
BestGene{1}={'dd','tt',3,5}
Bestresult{1}={0.022,{'dd','tt',3,5}}
BestFitness{2}=1.223;
BestGene{2}={'ccc','sss',13,25}
Bestresult{2}={1.223,{'ccc','sss',13,25}}
BestFitness{3}=0.003;
BestGene{3}={'d1d','t1t',31,51}
Bestresult{3}={0.003,{'d1d','t1t',31,51}}
the result of Result=min(BestFitness{:}) The answer should be {'d1d','t1t',31,51} Looking for your help
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 17 Dez. 2016
Try this:
BestGene={}
BestFitness={}
Bestresult={}
BestFitness{1}=0.022;
BestGene{1}={'dd','tt',3,5}
Bestresult{1}={0.022,{'dd','tt',3,5}}
BestFitness{2}=1.223;
BestGene{2}={'ccc','sss',13,25}
Bestresult{2}={1.223,{'ccc','sss',13,25}}
BestFitness{3}=0.003;
BestGene{3}={'d1d','t1t',31,51}
Bestresult{3}={0.003,{'d1d','t1t',31,51}}
celldisp(Bestresult)
% Use brackets or cat(1,BestFitness{:}) to concatenate all values into single vector.
[minValue, indexOfMin] = min([BestFitness{:}])
% Extract second cell of Bestresult's indexOfMin location.
% First need to extract the cell
finalResult = Bestresult(indexOfMin)
% This is a 1x2 cell. Need to get the second cell of it.
finalResult = finalResult{1}(1,2)
% Print to command window:
celldisp(finalResult)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!