# How do I find unique points in an array in a certain context?

조회 수: 4 (최근 30일)
FsC 2023년 4월 28일
댓글: FsC 2023년 5월 1일
Hello,
I am trying to find unique points in an array but within a unique context.
Say I have the following arrays consisting of x and y coordinates at spcific time points:
time = [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]';
x_coord = [ 1 1 1 rand(1,6) 1 1 1 rand(1,5) 1 1]';
y_coord = [[ 1 1 1 rand(1,6) 1 1 1 rand(1,5) 1 1]';
mat = [x_coord y_coord];
I want to find the unique x,y coordinates so that the first value of the '1's is preserved. Specifically, the unique X/Y point indices would be:
ia = 1, 4, 5, 6, 7, 8, 9,10,13,14,15,16,17,18; (essentially preserves the 1s that occur later on in the matrix)
when using the unique() function, it removes the 1s later on in the matrix.
is there a way to find the unique points but preserve the same points that occur later in time?
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Walter Roberson 2023년 4월 28일
Are you sure that you want unique() ? unique() is talking about the global situation -- so for example [x1, y1; x2, y2; x1, y1] the second x1, y1 would be considered duplicate .
It sounds to me as if it is more you are concerned whether you are in a run of duplicate values, which is a different task that can be determined with local information.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Image Analyst 2023년 4월 28일
편집: Image Analyst 2023년 4월 28일
Try this:
time = [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]';
x_coord = [ 1 1 1 rand(1,6) 1 1 1 rand(1,5) 1 1]';
y_coord = [ 1 1 1 rand(1,6) 1 1 1 rand(1,5) 1 1]';
mat = [x_coord, y_coord]
mat = 19×2
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2902 0.7648 0.9712 0.7973 0.9503 0.5313 0.5635 0.9937 0.1493 0.0628 0.1966 0.1561 1.0000 1.0000
all1rows = all(mat == 1, 2); % Find out which rows have 1 in every column.
% Throw out row if the prior row is all 1s.
% There is a vectorized way but I think you might find the for loop more intuitive.
rowsToKeep = true(numel(x_coord), 1);
for row = 2 : numel(all1rows)
if (all1rows(row) == 1) && (all1rows(row) == all1rows(row-1))
rowsToKeep(row) = false;
end
end
ia = find(rowsToKeep)'
ia = 1×14
1 4 5 6 7 8 9 10 13 14 15 16 17 18
mat = mat(rowsToKeep, :)
mat = 14×2
1.0000 1.0000 0.2902 0.7648 0.9712 0.7973 0.9503 0.5313 0.5635 0.9937 0.1493 0.0628 0.1966 0.1561 1.0000 1.0000 0.5499 0.2234 0.0796 0.7463
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
FsC 2023년 5월 1일
Perfect, this works! thank you!

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by