Find set of values that are unique to the values in first two columns in a table
18 次查看(过去 30 天)
显示 更早的评论
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 个评论
the cyclist
2019-11-24
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.
采纳的回答
Ridwan Alam
2019-11-24
编辑:Ridwan Alam
2019-11-24
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 个评论
dpb
2019-11-24
If going to use unique, use the 'rows' optional argument here.
Alternatively, look at findgroups
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!