Borrar filtros
Borrar filtros

Removing cells from a cell array that contain certain values for a particular element

22 visualizaciones (últimos 30 días)
Let me give an example. If I have a cell array:
A = {[1,1,1,1] [1,1,2,2] [1,1,3,3] [1,1,4,4] [2,1,1,1] [2,1,2,2] [2,1,3,3] [2,1,4,4] [3,1,1,1] [3,1,2,2]}
I want an efficient way to be able to remove entire cells where the 2nd and 4th elements of the cells both = 1
This should get rid of the 1st, 5th, and 9th cell, such that the output is:
A = {[1,1,2,2] [1,1,3,3] [1,1,4,4] [2,1,2,2] [2,1,3,3] [2,1,4,4] [3,1,2,2]}
Sorry if this is a naive question, I am still relatively new to Matlab.

Respuesta aceptada

the cyclist
the cyclist el 7 de Mzo. de 2017
Editada: the cyclist el 7 de Mzo. de 2017
Here's a one-liner:
A = A(not(cellfun(@(x)isequal(x([2 4]),[1 1]),A)));
The cellfun function applies a function to each element of a cell array. In this case, the function being applied is checking for the equality of the 2nd and 4th element against [1 1], as you want.
The output of cellfun is a logical index to the cells meeting that criterion. Then I select the elements of A that do not.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices 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