通过索引删除行 [英] Removing rows by indexing
问题描述
我有大约300行的单元格数组.该数组有6列.在第六列中,非常多的行带有零.我想从我的数组中删除这些行.
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.
我正在使用下面的行,但收到一条错误消息:类型为"cell"的输入参数的未定义函数"ne"."
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);
推荐答案
如果它是数字的单元格数组,请尝试以下操作-
If it's a cell array of numerals, try this -
myData(~vertcat(myData{:,6}),6)={[]}
或
myData(~cell2mat(myData(:,6)),6)={[]}
或者这,完全来自@chappjc的评论
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)={''}
如果在第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),:) = []
这篇关于通过索引删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!