How do I reduce a table by grouping elements with same x y z values?
1 view (last 30 days)
Show older comments
Hi,
I am trying to reduce a table by grouping the elements in column 1 which have equal elements in columns 2, 3 and 4
T = [1 1 3 5
1 1 3 6
2 1 3 5
3 3 4 5
3 4 6 7
2 3 4 5
4 3 4 5
2 1 3 6];
for U = 1:length(T)
indices{:,U}= find(T(:,2)==T(U,2) & T(:,3)==T(U,3) & T(:,4)==T(U,4));
end
[Au, idx, idx2]=uniquecell(indices);
for i=1:length(Au)
T2{:,i}=T(Au{1,i},1)
end
T3 = T2';
table = cell2table(T3);
[t,ia]=unique(T(:,2:4),'rows');
t3 = T(sort(ia),(2:4));
T3T = array2table(t3);
TABLE = [table T3T];
This works for me giving
[1;2] 1 3 5
[1,2] 1 3 6
[3;2,4] 3 4 5
[3] 4 6 7
I have a bigger table called tabr which I have attached, containing:
element number X1 PX1 PY1 PZ1 X2 PX2 PY2 PZ2
I am using the code below to group element numbers with same X1 PX1 PY1 PZ1 X2 PX2 PY2 PZ2 but uniquecell.m (attached) is not working for me. Could you please help me with it? Thanks in advance
tabr = table2array(tabr);
for U=1:length(tabr)
indices{:,U}=find(tabr(:,2)==tabr(U,2) & tabr(:,3)==tabr(U,3) & tabr(:,4)==tabr(U,4) & tabr(:,5)==tabr(U,5) & tabr(:,6)==tabr(U,6) & tabr(:,7)==tabr(U,7) & tabr(:,8)==tabr(U,8) & tabr(:,9)==tabr(U,9))
end
[Aut,idx,idx2]=uniquecell(indices);
for i=1:length(Aut)
T2{:,i}=tabr(Aut{1,i},1);
end
T3=T2';
table = cell2table(T3);
[tl,ia]=unique(tabr(:,2:9),'rows');
t3t=tabr(sort(ia),2:9);
T3T = array2table(t3t);
TABLE = [table T3T];
Answers (0)
See Also
Categories
Find more on Other Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!