Comparing data in a matrix/table
2 views (last 30 days)
Show older comments
In the table above, focus on the elements of columns 3 and 4. Column 3 is the object number and Column 4 is its "group." I want to count how many times an object was grouped in both 1 and 2. Store it in a variable "count." The value of "count" based on the table above should be 2 because only object numbers 11 and 17 were grouped in both 1 and 2.
EDIT: To make indexing easier, the name of the table above shall be called TableA
2 Comments
Answers (1)
Ive J
on 1 Jan 2021
Edited: Ive J
on 1 Jan 2021
One way would be to use groupsummary:
tab =
a1 a2 a3 a4
________ ________ __ __
0.84913 0.046171 10 1
0.93399 0.097132 10 1
0.67874 0.82346 11 1
0.75774 0.69483 11 2
0.74313 0.3171 12 1
0.39223 0.95022 14 1
0.65548 0.034446 14 1
0.17119 0.43874 14 1
0.70605 0.38156 14 1
0.031833 0.76552 17 2
0.27692 0.7952 17 1
G = groupsummary(tab, {'a3','a4'})
a3 a4 GroupCount
__ __ __________
10 1 2
11 1 1
11 2 1
12 1 1
14 1 4
17 1 1
17 2 1
So, number of repeats in column a3 would the variable count:
[a3unique, ~, idx] = unique(G.a3, 'stable');
count = histcounts(idx, [1:numel(a3unique), inf]);
count
1 2 1 1 2
a3unique'
10 11 12 14 17
See Also
Categories
Find more on Matrix Indexing 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!