Find set of values that are unique to the values in first two columns in a table
18 views (last 30 days)
Show older comments
Elian Ricardo Gonzalez Beltran
on 24 Nov 2019
Commented: dpb
on 24 Nov 2019
I have a table like the following. Columns C1, C2, and C3. Based on the unique values on first two columns, I need to collect all the values in a vector. In the below table, I have 7 unique rows. For each unique row, lets say [3,1], i need to collect values from column C3 as a vector [2,5] etc. Also please note that the first column C1 is a categorical data. eg, date. Any help is appreciated. Thanks
C1 C2 C3
3 1 2
3 1 5
3 3 1
1 3 3
3 2 3
2 3 3
1 1 3
1 2 3
2 3 2
3 3 2
3 3 8
3 3 1
1 3 10
1 Comment
the cyclist
on 24 Nov 2019
It would be easier to help if you uploaded the actual table (or a representative sample) in a *.mat file. This will prevent us from having to guess at exactly how your data are stored, what the date type is, etc.
Accepted Answer
Ridwan Alam
on 24 Nov 2019
Edited: Ridwan Alam
on 24 Nov 2019
I believe what you are looking for is the unique() function, specially the z in the example below:
>> mytable
mytable =
5×3 table
Var1 Var2 Var3
___________ ____ ____
20-Nov-2019 92 57
21-Nov-2019 29 8
22-Nov-2019 76 6
22-Nov-2019 76 54
24-Nov-2019 39 78
>> [x,y,z]=unique(mytable(:,1:2))
x =
4×2 table
Var1 Var2
___________ ____
20-Nov-2019 92
21-Nov-2019 29
22-Nov-2019 76
24-Nov-2019 39
y =
1
2
3
5
z =
1
2
3
3
4
Using "z" you can sort out your third column values. One way to do:
newVar = {};
for i = 1:max(z)
newVar{i} = table2array(mytable(z==i,3))';
end
Please let me know how it goes.
1 Comment
dpb
on 24 Nov 2019
If going to use unique, use the 'rows' optional argument here.
Alternatively, look at findgroups
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!