minimum of different sized cell arrays using cell2mat error

조회 수: 4 (최근 30일)
Joseph Lee
Joseph Lee 2017년 11월 24일
답변: Jan 2017년 11월 24일
How do i find the overall minimum of a cell array, cell2mat gives error.
Y= {[100 200] [50 100] [20] [30 140];
[10 130] [40] [60 200] [30]};
min(cell2mat(Y))
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 78)
m = cat(1,m{:});

채택된 답변

Jos (10584)
Jos (10584) 2017년 11월 24일
cell2mat fails because not all cells of Y are the same size. But you do not need this step to find the minimum:
Y= {[100 200] [50 100] [20] [30 140];
[10 130] [40] [60 200] [30]};
minC = cellfun(@(c) min([c(:) ; Inf]), Y) % get the minimum of each cell, added Inf to deal with empty cells
minY = min(minC(:)) % get the global minimum
  댓글 수: 1
Joseph Lee
Joseph Lee 2017년 11월 24일
Thanks, that helped remove this additional step i used
Y = Y(~cellfun(@isempty, Y))

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

추가 답변 (2개)

Stephen23
Stephen23 2017년 11월 24일
>> min([Y{:}])
ans = 10
  댓글 수: 1
Jos (10584)
Jos (10584) 2017년 11월 24일
Nice. I thought of that too, but it will fail when some cells of Y are not row-vectors.

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


Jan
Jan 2017년 11월 24일
The fast C-Mex function FEX: Cell2Vec converts the contents of the cell to a vector independent from the shapes of the cell elements:
Y = {[100 200] [50 100] [20] [30 140]; ...
[10 130] [40] [60 200] [30]};
C = Cell2Vec(Y);
min(C)
With FEX: MinMaxElem you can search the minimal and maximal values also:
[Min, Max, MinIndex, MaxIndex, MinArg, MaxArg] = MinMaxElem(Y{:})
This searches the max and the indices also. NOTE: This is not faster than min and max of modern Matlab versions anymore. But perhaps more convenient for your case.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by