Removing cells from a cell array that contain certain values for a particular element
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponse acceptée
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.
0 commentaires
Plus de réponses (0)
Voir également
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!