Filter löschen
Filter löschen

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

22 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

the cyclist
the cyclist am 7 Mär. 2017
Bearbeitet: the cyclist am 7 Mär. 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.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by