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

22 次查看(过去 30 天)
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.

采纳的回答

the cyclist
the cyclist 2017-3-7
编辑:the cyclist 2017-3-7
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 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by