regroup identical coordinate pair

Hi everyone,
I've got vectors like this corresponding to the coordinates of a point:
X = [1;1;8;5;5;6]
Y = [2;2;5;6;6;7]
and would like to regroup points that have the same coordinates and have the following results :
R_X = [1;8;5;6];
R_Y = [2;5;6;7];
Are there any functions or way to concatenate that would help me to do that ?
Thanks

 Accepted Answer

I have no idea what your objective is, however the unique function will produce the result you posted:
RX = unique(X,'stable')
RY = unique(Y,'stable')
producing:
RX =
1
8
5
6
RY =
2
5
6
7

6 Comments

Thank you for you answer,
I have a matrix composed of X and Y: M=[X,Y]
And I would have the following result:
Res = [1,2;8,5;5,6;6,7]
And I would like also to found the indexes where the duplicates values were found for exemple :
For X=1 and Y=2 (row 1) I would have the indexes 1 and 2
For X=8 and Y=5 (row 2) I would have index 3 etc
The function unique gives me indexes
I have tried the function unique but it only gives me one index for the values
My pleasure!
This works:
M = [X Y];
[Res,~,ic] = unique(M, 'rows', 'stable')
ix = accumarray(ic, (1:size(M,1)).', [], @(x){x})
for k = 1:size(Res,1)
Out = [Res(k,:) [ix{k}].'] % Output Loop (Display Only º Discard)
end
producing:
Res =
1 2
8 5
5 6
6 7
and:
Out =
1 2 1 2
Out =
8 5 3
Out =
5 6 4 5
Out =
6 7 6
This also demonstrastes how to access the processed data.
.
Thank you, this worked perfectly,
A last question if it doesn’t bother you,
Each point that I have got different characteristics such as temperature and humidity for example (they are different for every point even if they have the same coordinates in X and Y).
Temp = [22;26;23;20;25;27]
Now that I got indexes, of the points that have the same coordinates I would like to build an array containing the corresponding temperature of these point like this :
{[22,26];[23];[20,25];[27]}
I’ve tried with a for loop but it doen’t fill the cell correctly
Thank you in advance
My pleasure!
That’s a minor variation on my previous code:
Temp = [22;26;23;20;25;27];
M = [X Y Temp];
[Res,~,ic] = unique(M(:,[1 2]), 'rows', 'stable')
ix = accumarray(ic, (1:size(M,1)).', [], @(x){M(x,3)})
for k = 1:size(Res,1)
Out = [Res(k,:) [ix{k}].'] % Output Loop (Display Only º Discard)
end
producing:
Out =
1 2 22 26
Out =
8 5 23
Out =
5 6 20 25
Out =
6 7 27
That appears to do what you requested.
EDIT — (7 Jun 2020 at 14:59)
To save all the results to a matrix:
MaxCols = max(cellfun(@numel,ix)); % Maximum Number Of Temperatures For Any Location
Out = NaN(size(Res,1),2+MaxCols); % Preallocate: ‘NaN’ Matrix
for k = 1:size(Res,1)
Out(k,1:(2+numel(ix{k}))) = [Res(k,:) [ix{k}].'];
end
Out % Display Results (Optional)
producing (for this example):
Out =
1 2 22 26
8 5 23 NaN
5 6 20 25
6 7 27 NaN
.
Thank you for you help !
As always, my pleasure!

Sign in to comment.

More Answers (1)

[~, unqIdx] = unique([X(:), Y(:)], 'rows','stable');
R_X = X(unqIdx);
R_Y = Y(unqIdx);

Categories

Asked:

kix
on 4 Jun 2020

Commented:

on 7 Jun 2020

Community Treasure Hunt

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

Start Hunting!