# How to seprate elements based on value counts In MATLAB

1 view (last 30 days)
Stephen john on 23 Jun 2022
Commented: Stephen john on 27 Jun 2022
Hello Everyone, I hope you are doing well.
I have the data in form of cell array shape 1x6 , in which each cell have specfic values. I have developed an algorithm which can first create histrogram of each cell. and then find counts of each unique values. After that it it compare the each unique value with other cell. if it equal it assign that value to to the cell which have large counts.
But i am facing a problem in alogrithm
• The output of argument val
• first one is when i compare the unique value of each cell if it equal i assign it to other cell.but when it assign the 0 is written in it place. as you can see below in cell 4, i have unique value of 5. when it assign to cell 2 , cell 4 have 0 value in it place. • The output of argument cnts
• Second one is The counts does not added . for example the unique value of 5 have 15 count already in cell 2. and 3 counts from cell 4 should be added in cell 2 so the total count is 18. but in my case it does not work.
• clc; clear all; close all;
% BindataF=cell(1,8)
for i = 1:6
T = emptyCell{i};
h1(i)=histogram(T,100000,'BinLimitsMode','manual','BinLimits',[0 3e8]);
BindataF{i}=h1(i).Data;
T1=BindataF{i};
Values=unique(T1);
counts=histc(T1(:),Values);
val{i}=Values;
cnts{i} = counts;
end
for k =1:6
c = cnts{k};
v = val{k};
z=k+1;
for j=z:6
temp = val{j};
cnt_temp = cnts{j};
for m = 1: length(v)
for n = 1: length(temp)
if v(m)== temp(n)
c(m)=c(m)+cnt_temp(n);
temp(n)= 0;
cnt_temp(n) = 0;
end
end
end
val{j} = temp;
cnts{j} = cnt_temp;
end
end
Stephen john on 27 Jun 2022
@dpb The desired results is the count should be added to already present count

Karim on 23 Jun 2022
Edited: Karim on 23 Jun 2022
If i understand it correctly, you want to keep only the unique values from each cell and find the overal "count" of each value?
If so, i tried to adjust the code accordingly
numCells = length(emptyCell);
uVal = cell(1,numCells);
uCounts = cell(1,numCells);
for i = 1:numCells
currCell = emptyCell{i};
[uVal{i},~,uLoc] = unique(currCell);
uCounts{i} = accumarray(uLoc, 1);
for j = 1:i-1
[iLoc,jLoc] = ismember(uVal{i},uVal{j});
if any(iLoc)
% add the counts
uCounts{j}(logical(jLoc)) = uCounts{i}(iLoc);
% remove the value
uVal{i}(iLoc) = [];
uCounts{i}(iLoc) = [];
end
end
end
for some reason i cannot add "Dataset.mat", because i reach my daily upload limit... we are only allowed to upload 10 attachments a day. Hence i cannot run the code n the answer.
Anyhow, here you can see print screens from the result on my pc: R2021b

### Community Treasure Hunt

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

Start Hunting!