Find and Save Values of Unique Combinations of Two Rows of a Matrix

6 ビュー (過去 30 日間)
Amy Eisenstadt
Amy Eisenstadt 2017 年 6 月 1 日
編集済み: Niko 2017 年 6 月 2 日
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.

採用された回答

Niko
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 件のコメント
Amy Eisenstadt
Amy Eisenstadt 2017 年 6 月 2 日
編集済み: Amy Eisenstadt 2017 年 6 月 2 日
Hi Niko, this worked aside from a small blip at the beginning of the output matrix. The first few values of the output matrix show repeated long and lat values instead of unique ones, although this is a small error I can fix manually. Additionally, would you mind explaining to me what the '[]' does and why you placed an '@' in front of 'mean'? I'd love to be able to understand more- thank you so much for taking the time to help me!
Niko
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 ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by