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

15 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

the cyclist
the cyclist le 7 Mar 2017
Modifié(e) : the cyclist le 7 Mar 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.

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by