2
votes

I have a cell array it is 100x1, variable name is CellA. As you will see from the png file attached, the cell array contains various size matrices in each of the 100 different cells.

I want to extract that data. Actually, I am trying to find the number of unique elements in each cell and their frequency. Below is my code which gets dimensional errors:

for i=1:length(CellA)
 if isempty(CellA{i})
     continue;% do nothing
 else
     unqmz(i,:) = unique(CellA{i})';
     countmz(i,:) = histc(CellA{i}, unqmz(i))';
 end

Eventually, I want to plot count versus unique number for each different cell array where the total number of counts for that cell exceeds a pre-determined value. e.g.) 4

cell snippet

2

2 Answers

1
votes

You can also do it this way:

unqmz = cellfun(@unique, CellA, 'uni', 0);
countmz = arrayfun(@(n) histc(CellA{n},unqmz{n}), 1:numel(CellA), 'uni', 0).';
1
votes

See if this works out for you -

%// Get result data into unqmz and countmz
unqmz = cell(numel(CellA),1);
countmz = cell(numel(CellA),1);

for ii=1:numel(CellA)
    if isempty(CellA{ii})
        continue;% do nothing
    else
        unqmz(ii) = {unique(CellA{ii})'} %//'
        countmz(ii) = {histc(CellA{ii}, cell2mat(unqmz(ii)))'} %//'
    end
end

%// Count of number of unique numbers in each cell
count_uniqs = cellfun(@numel,unqmz);

%// Plot (assuming you want to plot everything in one figure window)
figure,
for k = 1:size(countmz,1)
    if count_uniqs(k)>4
        plot(cell2mat(unqmz(k)),cell2mat(countmz(k)),'x')
        hold on
    end
end