Home > Enterprise >  Counting number of 'negative' cells for each ID
Counting number of 'negative' cells for each ID

Time:01-10

I want to count how many 'negative' cells there are for each unique ID. What am I doing wrong here? I then want to display how many ID's have one or more negative counts and how many ID's have three or more negative counts.

my code:

clc
clear
close all

data = {
'vajbfch07wu11y90'  'negative'
'ekvpmhqybu920hnr'  'negative'
'eddbdv5bsqggeydq'  'negative'
'eddbdv5bsqggeydq'  'negative'
'eddbdv5bsqggeydq'  'negative'
'eddbdv5bsqggeydq'  'negative'
'eddbdv5bsqggeydq'  'negative'
'eddbdv5bsqggeydq'  'negative'
'6vmhbj7041fe7xck'  'negative'
'6vmhbj7041fe7xck'  'negative'
'6vmhbj7041fe7xck'  'negative'
'6vmhbj7041fe7xck'  'negative'
'j5d6dgzbeynyyg02'  'negative'
'j5d6dgzbeynyyg02'  'negative'
'j5d6dgzbeynyyg02'  'negative'
'v21tstcp164uya6g'  'negative'
'v21tstcp164uya6g'  'negative'
'brjmfpunj00rn92c'  'negative'
'brjmfpunj00rn92c'  'negative'
'mb8nggnegcwq9nqc'  'negative'
'vk4ga34492m01hwv'  'negative'
'vk4ga34492m01hwv'  'negative'
'vk4ga34492m01hwv'  'negative'
'vk4ga34492m01hwv'  'negative'
'd9hk2zeexhxp2h0v'  'negative'
'f93xk5sq60ehp34j'  'negative'
'f93xk5sq60ehp34j'  'negative'
'ypzzn212hqvwjtc9'  'negative'
'0q2mmnq0wb97z7bm'  'negative'
'jh8k1dd9g2p2d218'  'negative'
'2e5tr0scw89z68kg'  'negative'
'2e5tr0scw89z68kg'  'negative'
'5zb72reqnsxnzuca'  'negative'
'5zb72reqnsxnzuca'  'negative'
'5zb72reqnsxnzuca'  'negative' };



[sigma, ~, uidx] = unique(data(:,1));
counts = accumarray([uidx, 2-data(:,2)], 1);
sigma = [sigma, counts];
sigma

error message:

Operator '-' is not supported for operands of type 'cell'.

Error in count_ex (line 45)
counts = accumarray([uidx, 2-data(:,2)], 1);

CodePudding user response:

You could use the new dictionary type in R2022b to count occurences and at the same time get unique items:

d = dictionary(data(:, 1), 0);
for entry = transpose(data(:, 1)); 
    d(entry) = d(entry)   1;
end

If that is not available: It seems all entries are labeled 'negative', so you just need to count occurences in column one, see https://de.mathworks.com/matlabcentral/answers/115838-count-occurrences-of-string-in-a-single-cell-array-how-many-times-a-string-appear

c = categorical(data(:, 1));
[categories(c), num2cell(countcats(c))]
  • Related