I have:
- {A} which is a 100x1 cell with each cell element being a 44x1 matrix.
- {B} which is a 100x1 logical cell with each cell element being a 44x1 logical matrix.
I would like to know if there is a way to apply {B} to {A}, cell element by cell element, in order to filter the matrices contained in cell {A}.
Thanks

 Respuesta aceptada

Guillaume
Guillaume el 27 de Jun. de 2018

1 voto

result = cellfun(@(a, b) a(b), A, B, 'UniformOutput', false)
will produce a 100x1 cell array of vectors (since filtering a matrix with a logical array always result in vectors).

8 comentarios

simone sangaletti
simone sangaletti el 27 de Jun. de 2018
thank you, it works great!!!
Although in one case {A} is a 100x1 cell with each cell element being a 44x3 matrix and I'd like to perform the same operation obtaining a 100x1 cell with each element being a matrix with 3 coloumns.
How can I do it?
Guillaume
Guillaume el 27 de Jun. de 2018
The code above makes no assumption about the size of the arrays in each cell. As long as the corresponding logical array is the same size, the code will work. You could have
A = {44x1 double, 44x3 double, 10x10x3 double, ...}
As long as
B = {44x1 logical, 44x3 logical, 10x10x3 logical, ...}
it will work.
simone sangaletti
simone sangaletti el 27 de Jun. de 2018
Editada: simone sangaletti el 27 de Jun. de 2018
it is not possible to do it with:
A = {44x3 double,44x3 double,44x3 double,........}
B = {44x1 logical,44x1 logical,44x1 logical,........}
Is it?
Guillaume
Guillaume el 27 de Jun. de 2018
It probably can be done depending on how you define the filtering of a 44x3 matrix by a 44x1 logical. Should all 3 rows be filtered with the same 44x1 array?
simone sangaletti
simone sangaletti el 27 de Jun. de 2018
yes please, thank you
Guillaume
Guillaume el 27 de Jun. de 2018
Editada: Guillaume el 27 de Jun. de 2018
assumption: each cell in B contains a logical row vector with the same number of columns as the corresponding array in A
result = cellfun(@(a, b) a(repmat(b, size(a, 1), 1), A, B, 'UniformOutput', false)
The above simply replicates the logical row vector to match the number of rows in b.
simone sangaletti
simone sangaletti el 27 de Jun. de 2018
perfect, it works!!! thanks
Mack Gardner-Morse
Mack Gardner-Morse el 25 de Mzo. de 2022
However, it is not any faster than just doing the for loop. result = cell(100,1); for k = 1:100; result{k} = A{k}(B{k}); end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Operators and Elementary Operations en Centro de ayuda y File Exchange.

Productos

Versión

R2017b

Preguntada:

el 27 de Jun. de 2018

Comentada:

el 25 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by