MATLAB: How to calculate index of minimum value in cell array

minimum value

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…

Best Answer

>> 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