mHelpMe
mHelpMe

Reputation: 6668

Removing rows by indexing

I have cell array of approx 300 rows. The array has 6 columns. In the 6th column very are lots of rows with zeros. I want to remove these rows from my array.

I am using the line below but getting an error message saying "Undefined function 'ne' for input arguments of type 'cell'."

myData = myData(myData(:,6) ~= 0);

Upvotes: 1

Views: 91

Answers (1)

Divakar
Divakar

Reputation: 221624

If it's a cell array of numerals, try this -

myData(~vertcat(myData{:,6}),6)={[]}

or

myData(~cell2mat(myData(:,6)),6)={[]}

or this, which is purely from @chappjc's comments

myData(~[myData{:,6}],6)={[]}

If it's a cell array of characters, try this -

myData(~str2double(myData(:,6)),6)={''}

Edit 1: If you would like to remove entire rows if the corresponding elements in the 6th column are zeros, index the whole row using :. Thus, the above codes would change to the following forms respectively :

myData(~vertcat(myData{:,6}),:)={[]}

myData(~cell2mat(myData(:,6)),:)={[]}

myData(~[myData{:,6}],:)={[]}

myData(~str2double(myData(:,6)),:)={''}

Edit 2: If you would like to remove the rows from the cell array that have all empty cells, you may use this -

myData(all(cellfun('isempty', myData),2),:) = []

Upvotes: 2

Related Questions