2

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 Answers 2

1

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).';
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Luis. Very neat. One other side question. Using line of code from @Divakar . If we consider just one single plot, at say k = 10 for example. plot(cell2mat(unqmz(10)),cell2mat(countmz(10)),'x') This plot will start and end with data points whose count is atleast 1. I would like to add one additional value to the start and end of unqmz. The value added at the start will be unqmz(1) - DELTA. The value added at the end will be unqmz(end) + DELTA. Similarly adding one entry at the start and end of countmz whose value will be 0. Any neat way of achieving this??? Thanks again!
First define a variable tmp = cell2mat(unqmz(10));. Then use something along these lines: plot([tmp(1)-DELTA; tmp; tmp(end)+DELTA],[0; cell2mat(countmz(10)); 0],'x')
1

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

3 Comments

The only issue (which is down to my poor explanation) is with the pre-determined value. I don't want to check if the number of uniques in a particular cell entry is greater than four. I want to check if for any particular cell entry the count exceeds four. i.e.) a cell entry may have more than 4 unique entries but they may all have a count of 1. I want to check if the count if greater than 4 irrespective of the number of unique entries.
I tried this but with dimensional errors (not sure why?): for i = 1:length(countmz)%size(countmz,1) maxcount(i) = max(cell2mat(countmz(i))); end
@user1011182 Try this - countmz(cellfun(@isempty, countmz))={[0]} and then maxcount = cell2mat(cellfun(@max, countmz,'uni',0))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.