How to calculate index of minimum value in cell array
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jyothi Alugolu
am 9 Mai 2017
Beantwortet: Jan
am 9 Mai 2017
Hello, I have a cell array by name m1 of size 374 * 3 (where m1(1,1}=1 * 8,m1{1,2}=1*8 and m1{1,3}=1*1..totally there are 17 cells)..Now i want minimum value index from all this cells... for example if m1{1,1}=[0.34463 0.3276 0.3615 0.3446 0.3559 0.3389 0.3389 0.3446], m1{1,2}=[0.36723 0.3333 0.3615 0.3615 0.3446 0.3559 0.3163 0.3333] and m1{1,3}=[0.3390] ....now minimum value is 0.3163 and it's index value is m1{1,2}(1,7)... so,likewise i want the minimum index value from all 374 cells...
0 Kommentare
Akzeptierte Antwort
Stephen23
am 9 Mai 2017
Bearbeitet: Stephen23
am 9 Mai 2017
>> m1{1,1} = [0.34463,0.3276,0.3615,0.3446,0.3559,0.3389,0.3389,0.3446];
>> m1{1,2} = [0.36723,0.3333,0.3615,0.3615,0.3446,0.3559,0.3163,0.3333];
>> m1{2,1} = [0.3390];
>> m1{2,2} = 5:9;
An efficient method is to use min once:
>> [val,idx] = min([m1{:}]);
>> len = [0;cumsum(cellfun('length',m1(:)))];
>> idc = find(len>=idx,1,'first')-1; % cell index
>> idv = idx-len(idc); % vector index
The minimum is thus:
>> val
val = 0.31630
and the minimum can be obtained using those indices:
>> m1{idc}(idv)
ans = 0.31630
You can get the cell array row and column indices by using ind2sub:
>> [row,col] = ind2sub(size(m1),idc)
row = 1
col = 2
8 Kommentare
Stephen23
am 9 Mai 2017
Bearbeitet: Stephen23
am 9 Mai 2017
"if the value of idc,idv is 2,7 means i.e 2nd column 7th cell...if idc,idv value is 3,1 means 3rd column ..."
No they do not.
Both idc and idv are linear indices, which means that if idc is seven it will be the seventh cell in the call array (assuming numel>=7). The value of idc does not tell you the column of the cell containing the minimum! (But I did show you how to get the row and column indices, by using ind2sub. Personally I would not bother).
And the index idv is the index of position in the numeric vector within that cell, so this is certainly not the "7th cell" or "1st cell", because idv does not refer to cells at all but elements of the numeric vector inside that cell.
My code does not use row or column indices at all, so be careful that you do not incorrectly interpret those linear indices. You can learn more about linear indices by reading the documentation (note that row and column indices are called subscript indices):
Weitere Antworten (2)
KSSV
am 9 Mai 2017
m1{1,1}=[0.34463 0.3276 0.3615 0.3446 0.3559 0.3389 0.3389 0.3446] ;
m1{1,2}=[0.36723 0.3333 0.3615 0.3615 0.3446 0.3559 0.3163 0.3333] ;
m1{1,3}=[0.3390] ;
iwant = cell(size(m1)) ;
for i = 1:length(m1) ;
[idx,val] = min(m1{i}) ;
iwant{i} = [idx,val] ;
end
3 Kommentare
Jan
am 9 Mai 2017
[MinValue, MaxValue, MinIndex, MaxIndex, MinArg, MaxArg] = MinMaxElem(m1{:});
In older Matlab version this was faster than calling min and max separately, but modern Matlab versions are more efficient.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!