Remove columns from cell array when several cells in such columns have zero-values

2 次查看(过去 30 天)
Hi all,
I use R2019b. I have a cell array (11 x 400) in which the first row and first column contain words (e.g. 'Person#001', 'Person#002', ... 'Person#399' for the first row and 'ITEM1', 'ITEM2', ... 'ITEM10' for the first column). The remainder of the cells (2:end,2:end) are occupied by numerical values that range from 0 to 1. This dataset represents the amounts of each of the 10 items belonging to each of the 399 persons.
Some of these persons have only 0 values for all of their respective numerical values. I am trying to remove the columns from the cell array that contain only zero numerical values in rows 2 through 11 (i.e. for all 10 items). In the example below (that represents a condensed version of my data), I want the columns with 'Person#004' and 'Person#006' at the top to be removed entirely. There are several of these all-zero columns throughout the entire dataset and ideally this script would remove all of them.
Thanks in advance for your help!
BEFORE
'Person#001' 'Person#002' 'Person#003' 'Person#004' 'Person#005' 'Person#006' . . . 'Person#399'
'ITEM1' 0 0.2 0.4 0 0.1 0 0.4
'ITEM2' 0 0 0.2 0 0 0 0.1
'ITEM3' 0.5 0 0.1 0 0 0 0
'ITEM4' 0 0.1 0 0 0 0 0.3
AFTER
'Person#001' 'Person#002' 'Person#003' 'Person#005' . . . 'Person#399'
'ITEM1' 0 0.2 0.4 0.1 0.4
'ITEM2' 0 0 0.2 0 0.1
'ITEM3' 0.5 0 0.1 0 0
'ITEM4' 0 0.1 0 0 0.3

采纳的回答

Adam Danz
Adam Danz 2020-6-10
编辑:Adam Danz 2020-6-10
cellArray is your cell array.
threshold is the percent of 0-values per column that defines "several".
See inline comments for details.
% Define a threshold that defines "several".
% For example. 0.5 means 50% of the values in a column is 0 (ignoring 1st row)
% 1.0 means 100% of the values in a column is 0 (ignoring 1st row)
threshold = 0.5;
% Compute the percent of 0-values per column in rows 2:end (pZero)
isZero = cellfun(@(x) isnumeric(x) && x==0, cellArray(2:end,:)); % <--- UPDATED FROM ORIGINAL ANSWER
pZero = mean(isZero,1);
% Remove columns with "several" zeros
cellArray(:,pZero >= threshold) = [];
If you want to retain the original cell array, copy it to a different variable before the final line.
  4 个评论
flameohotman
flameohotman 2020-6-11
Thank you so much Adam. I'm now getting what I was hoping for. I am still getting a grasp of MATLAB so I am grateful for the support!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by