How to select data from a cell array based on some condition

15 visualizaciones (últimos 30 días)
Hedayat
Hedayat el 31 de Ag. de 2019
Comentada: Hedayat el 5 de Sept. de 2019
Hi, I am facing a problem with cell array, I have a data, for example, A as below
A = {[0, 5], [-2, 3]; [0,0], [12,-21]};
And I want the output as a cell/array of values greater than 2, like this-
B = {[5], [3]; [0], [12]}
I tried this way but its not working
K = @(data, fn) cellfun(@(x) x(data{:}), fn,'UniformOutput',0);
B = K(A, {@(x) x > 2})
I would appreciate if anybody help me with this
  2 comentarios
Stephen23
Stephen23 el 1 de Sept. de 2019
"I want the output as a cell/array of values greater than 2, , like this- B = {[5], [3]; [0], [12]}"
How is zero greater than two ?
Hedayat
Hedayat el 1 de Sept. de 2019
Sorry for not mentioning the case when it doesn’t satisfy the condition(both elements are zero). It should give me either zero or NaN.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 2 de Sept. de 2019
>> A = {[0,5], [-2,3]; [0,0], [12,-21]};
>> B = cellfun(@(v)v(v>2),A,'Uni',0);
>> B(cellfun('isempty',B)) = {NaN}
B =
[ 5] [ 3]
[NaN] [12]

Más respuestas (1)

Walter Roberson
Walter Roberson el 31 de Ag. de 2019
B = cellfun(@(data) max(data), A, 'uniform', 0);
C = cellfun(@(data) min(data), A, 'uniform', 0);
  1 comentario
Hedayat
Hedayat el 31 de Ag. de 2019
Thanks Walter, but I actually needed to do more operation on my data so I edited the question to be more general. If you could help me with this too.

Iniciar sesión para comentar.

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by