How to calculate index of minimum value in cell array

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

 Respuesta aceptada

Stephen23
Stephen23 el 9 de Mayo de 2017
Editada: Stephen23 el 9 de Mayo de 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 comentarios

Thanks for the reply...i got 'val' value.... but i am not getting idc and idv value..error is at len = [0,cumsum(cellfun(@numel,m1))]; ..error is Error using horzcat Dimensions of matrices being concatenated are not consistent... Am sending you the screenshots regarding m1 cell array..
Stephen23
Stephen23 el 9 de Mayo de 2017
Editada: Stephen23 el 9 de Mayo de 2017
@Jyothi Alugolu: use this line for a matrix cell array:
len = [0;cumsum(cellfun('length',m1(:)))];
See my edited answer.
yeah i got..thank you....but KSSV sir code is bit easy to implement and to understand...can you help me with that code..
Well, do you want code that works, do what you asked, and is actually not that complicated (Stephen's) or some code that is a bit easy to implement and to understand but doesn't do what you want (KSSV's as it is)?
Most likely, Stephen's code will also be faster than KSSV's once it's fixed to do what you want.
now by this index value how i can i retrieve cell which consists of binary values..If suppose, i have a cell array by name Res of size 374 * 3...now i want to pick the cell from each 1 * 3 cell array by the index value in [idc idv]....
@Jyothi Alugolu: well, apart from the fact that Res has the same size as m1, you have not told us any useful information: what are the "1 * 3 cell array"? What does it mean to to "pick the cell from each" of them?
In any case you have the cell array and vector indices, so have you tried using them?:
Res{idc}
Res{idc}(idv)
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 el 9 de Mayo de 2017
Editada: Stephen23 el 9 de Mayo de 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):

Iniciar sesión para comentar.

Más respuestas (2)

KSSV
KSSV el 9 de Mayo de 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 comentarios

Hello KSSV sir, i got val value...but idx value must be {1,7}..i.e m1{1,7} is the minimum index value...
Mainly i want index value because the index value is further used for some other purpose...
KSSV
KSSV el 9 de Mayo de 2017
What do you think is idx in the above code?

Iniciar sesión para comentar.

Jan
Jan el 9 de Mayo de 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.

Etiquetas

Preguntada:

el 9 de Mayo de 2017

Respondida:

Jan
el 9 de Mayo de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by