How to calculate index of minimum value in cell array

20 views (last 30 days)
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...

Accepted Answer

Stephen23
Stephen23 on 9 May 2017
Edited: Stephen23 on 9 May 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 Comments
Jyothi Alugolu
Jyothi Alugolu on 9 May 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 1st cell..but 3rd column i.e m{1,3} contains only single value..(as i told u previously when i asked the question).. if idc,idv is 2,4..then it is 2nd column 4th cell in Res{1,2}(1,4)(for suppose)....likwise,if idc,idv is 3,1,then value is not returning...
Stephen23
Stephen23 on 9 May 2017
Edited: Stephen23 on 9 May 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):

Sign in to comment.

More Answers (2)

KSSV
KSSV on 9 May 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 Comments
Jyothi Alugolu
Jyothi Alugolu on 9 May 2017
Mainly i want index value because the index value is further used for some other purpose...
KSSV
KSSV on 9 May 2017
What do you think is idx in the above code?

Sign in to comment.


Jan
Jan on 9 May 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.

Categories

Find more on Audio Processing Algorithm Design in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!