Find and Save Values of Unique Combinations of Two Rows of a Matrix
3 次查看(过去 30 天)
显示 更早的评论
I am working with a matrix that has three rows, the 1st row being longitude data, and the 2nd row being latitude data. I would like to be able to save out the unique combinations of the longitude and latitude values to a separate matrix. I have seen many posts indicating how to achieve the index positions of the unique combinations, although I need to be able to see the literal values of these combinations, not just their indexes. For example:
Row 1: 1 1 1 1 1 3 3 4 4 5 5 5 5 6 6
Row 2: 2 2 3 3 4 4 4 4 5 5 6 6 7 7 8
Dimensions: 2x15
Both of the row vectors have the same number of entries, and my end goal is to be able to produce this new matrix:
Row 1: 1 1 1 3 4 4 5 5 5 6 6
Row 2: 2 3 4 4 4 5 5 6 7 7 8
Dimensions: 2x11
Please note that these two rows, latitude and longitude, are pulled out of a larger data set that contains other information as well. Ultimately, I would like to be able to take the average of the values of a third row, Row 3, that correspond to each unique combination of Row 1 and 2. For example (maintaining the same rows as above and adding a third):
Row 1: 1 1 1 1 1 3 3 4 4 5 5 5 5 6 6
Row 2: 2 2 3 3 4 4 4 4 5 5 6 6 7 7 8
Row 3: 3 4 6 2 1 3 4 5 7 4 3 1 3 2 1
Where the end result is now a 3x11 matrix:
Row 1: 1 1 1 3 4 4 5 5 5 6 6
Row 2: 2 3 4 4 4 5 5 6 7 7 8
Row 3: 3.5 3 1 3.5 5 7 4 2 3 2 1
I have attempted to use the unique function, although to no avail, since I am unsure how to use this function for a combination of two rows. I have been successful in calculating the average of Row 3 values when I simply add together Row 1 and 2 into a sum, although this does not preserve the latitude and longitude data. Any help is greatly appreciated, and thank you for your time! Please let me know if I should attach code to this question in order to receive help.
0 个评论
采纳的回答
Niko
2017-6-1
Input:
A = [1 1 1 1 1 3 3 4 4 5 5 5 5 6 6
2 2 3 3 4 4 4 4 5 5 6 6 7 7 8
3 4 6 2 1 3 4 5 7 4 3 1 3 2 1];
You can try
[B, ~, idx] = unique(A(1:2, :)', 'rows', 'stable');
output = [B, accumarray(idx, A(3, :)', [], @mean)]';
2 个评论
Niko
2017-6-2
编辑:Niko
2017-6-2
Hi Amy, for the repeating columns, are the long and lat values identical or do they actually differ by a small fraction? My guess is they are slightly different so unique did not merge them correctly. You can try typing
format long
in command line to see more digits of the numbers, and if this is the case, maybe your should consider preprocessing your data (round them to the fifth decimal place, etc.) before progressing to the data analysis, or use the uniquetol function instead of unique.
The accumarray function gathers elements by their assigned indices (idx in the code) and performs some operation on each group. the [] means we are not explicitly specifying the size of the output and the @ returns a function handle of the specified function.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!