rudresh dwivedi
rudresh dwivedi

Reputation: 113

How to select the rows based on number of columns in a cell matrix?

I have a cell matrix of size 248*15 with the maximum number of columns 15. I want to extract the rows containing greater than or equal to 8(>=8) non-zero column entries in MatLab.

For example: cell row 1,2,7,8,.....

Find attached image

Upvotes: 0

Views: 50

Answers (1)

crazyGamer
crazyGamer

Reputation: 1139

You can use cellfun to first determine which cell elements are empty, and then use array indexing to select the rows as required:

C = {} % The cell matrix of size 248 x 15.

% An array of 248 x 15 that has Booleans based on empty or not:
emptyCells = cellfun(@isempty, C)

% The total number of empty columns on each row:
emptyColsCount = sum(emptyCells, 2)

% Find those rows with at least 8 non-zero columns
requiredRowIndices = find(emptyColsCount < 8)
% This returns [1, 2, 7, ...]

Upvotes: 1

Related Questions