Count element of cell array that are names

2 views (last 30 days)
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 Comment
Jan
Jan on 17 Jan 2018
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{:})?

Sign in to comment.

Accepted Answer

Alexandre Mangot
Alexandre Mangot on 18 Jan 2018
I was able to re-arrange Jan's code to make it work. For other in needs, this is how it works. I created the Pool manually as my data names were changing and were not a series. The typo mistake just comes from having 2 commas in histcounts as well as locB has the first l uppercase, therefore inconsistent with the line before.
I kept the data in the original 1x24 cell array containing the different SIZEx1 cell arrays on row 1 inside of it. Then I apply the following code:
TypeNames = {'Car1', 'Car2', 'Car3', 'Moto1', 'Moto2', 'Bike', 'Bus', 'Walk'};
for i = 1:length(type)
[~, locB] = ismember(type{:,i}, TypeNames);
TypeCount{i} = histcounts(locB, 'BinMethod', 'Integers');
end
  2 Comments
Jos (10584)
Jos (10584) on 18 Jan 2018
TypeCount = arrayfun(@(k) cellfun(@(c) sum(strcmp(c, type{k})), Typenames), 1:numel(type), 'un', 0)
Alexandre Mangot
Alexandre Mangot on 18 Jan 2018
Actually, Some of the Type are not present in some of the different cell array (this is normal coming from the data). However, TypeCount sometimes just returns 0 when no type name matches which is good, but is not consistently doing that. When it's not counting 0, it just doesn't store it in the array and just skips it. Therefore I'm ending with TypeCount being of different size.
Is there a way or reason for that? I checked my data type and they are all consistent. It's blocking from further analysis as I need to have all of them be the same length as TypeNames.

Sign in to comment.

More Answers (1)

Jan
Jan on 17 Jan 2018
Edited: Jan on 17 Jan 2018
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 Comments
Jan
Jan on 19 Jan 2018
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.
Alexandre Mangot
Alexandre Mangot on 19 Jan 2018
Yes thank you. I'm still having the issue I wrote in a comment above though.

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!