Borrar filtros
Borrar filtros

minimum of different sized cell arrays using cell2mat error

1 visualización (últimos 30 días)
Joseph Lee
Joseph Lee el 24 de Nov. de 2017
Respondida: Jan el 24 de Nov. de 2017
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{:});

Respuesta aceptada

Jos (10584)
Jos (10584) el 24 de Nov. de 2017
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 comentario
Joseph Lee
Joseph Lee el 24 de Nov. de 2017
Thanks, that helped remove this additional step i used
Y = Y(~cellfun(@isempty, Y))

Iniciar sesión para comentar.

Más respuestas (2)

Stephen23
Stephen23 el 24 de Nov. de 2017
>> min([Y{:}])
ans = 10
  1 comentario
Jos (10584)
Jos (10584) el 24 de Nov. de 2017
Nice. I thought of that too, but it will fail when some cells of Y are not row-vectors.

Iniciar sesión para comentar.


Jan
Jan el 24 de Nov. de 2017
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.

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by