How to calculate index of minimum value in cell array

조회 수: 7 (최근 30일)
Jyothi Alugolu
Jyothi Alugolu 2017년 5월 9일
답변: Jan 2017년 5월 9일
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...

채택된 답변

Stephen23
Stephen23 2017년 5월 9일
편집: Stephen23 2017년 5월 9일
>> 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
Jyothi Alugolu
Jyothi Alugolu 2017년 5월 9일
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 2017년 5월 9일
편집: Stephen23 2017년 5월 9일
"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):

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

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

댓글을 달려면 로그인하십시오.


Jan
Jan 2017년 5월 9일
[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.

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by