Comparing data in a matrix/table

조회 수: 1 (최근 30일)
Joaquin Lalusin
Joaquin Lalusin 2021년 1월 1일
댓글: Joaquin Lalusin 2021년 1월 1일
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에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by