Count element of cell array that are names
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a 1x24 cell array called "type" that contains on the first row multiple cell arrays that are of different size but always SIZEx1 cell. For instance, type{1,1} is a 2005x1 cell then type{1,2} is a 3176x1 cell. They all have 1 column. Data inside are of the class cell but are Names (Type1, Type2, Type3, ..., Type8). I am trying to count the amount of occurence of Type1 through Type8 in the 24 cell arrays.
What I have tried so far was to modified the class of my data to get strings and then use the function count. But I haven't found a way to do that.
I also tried to format the 1x24 cell array into a 3176x24 cell array using cellstr in a for loop (3176 being the longest column of my data). I was able to achieve that, however the data class is still cell and since all columns are not of the same length, [] was added to complete the 3176x24 cell array.
Any help would be appreciated (R2016a).
1 个评论
Jan
2018-1-17
I'm not sure if I understand the format of your inputs. Why did you try to create a 3176x24 cell array? What about: cat(1, type{:})?
采纳的回答
Alexandre Mangot
2018-1-18
2 个评论
Jos (10584)
2018-1-18
TypeCount = arrayfun(@(k) cellfun(@(c) sum(strcmp(c, type{k})), Typenames), 1:numel(type), 'un', 0)
更多回答(1 个)
Jan
2018-1-17
编辑:Jan
2018-1-17
Do you mean this:
% Join all cell strings:
c = cat(1, type{:});
% Create list of searched strings:
Pool = sprintfc('Type%d', 1:8); % {'Type1', 'Type2', ...}
% Count the occurrences:
[~, locB] = ismember(c, Pool);
Count = histcounts(LocB, , 'BinMethod', 'Integers')
?
3 个评论
Jan
2018-1-19
I have "locB" in one line and the typo "LocB" in the next line. I assume a reader can fix this.
I see, that you could modify my code easily by using a loop. So I'm glad, that I could help you.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!