Comparing data in a matrix/table

2 次查看(过去 30 天)
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 个评论
Ive J
Ive J 2021-1-1
This looks like a homework to me. What have you tried so far?
Joaquin Lalusin
Joaquin Lalusin 2021-1-1
Its not homework per se, more of a personal project.I have already processed the data I'm working with to result in the table above. I'm currently trying to index my data to figure out how many "objects" each "group" share. I've been racking my brain around for hours but haven't had any luck. I've been thinking of making a nested for and if loop but I cant think of the best way how.

请先登录,再进行评论。

回答(1 个)

Ive J
Ive J 2021-1-1
编辑:Ive J 2021-1-1
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
  1 个评论
Joaquin Lalusin
Joaquin Lalusin 2021-1-1
Thank you for your time! This isn't quite what I was aiming for but you gave me a pretty good idea of what to do anyways so thank you so much!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by