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

2 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Niko
Niko el 1 de Jun. de 2017
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 comentarios
Amy Eisenstadt
Amy Eisenstadt el 2 de Jun. de 2017
Editada: Amy Eisenstadt el 2 de Jun. de 2017
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 el 2 de Jun. de 2017
Editada: Niko el 2 de Jun. de 2017
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Preprocessing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by