How to Find Index of Repeated Elements in a Vector?

조회 수: 9 (최근 30일)
Stuart Shanks
Stuart Shanks 2021년 3월 8일
댓글: Stuart Shanks 2021년 3월 8일
I have a vector, for example:
x = [7, 22, 16, 20, 22, 3, 6, 22, 5]
I would like to first look the nth element of the vector, and then check the other indices to see if this value is repeated.
For example:
x(5) = 22.
How would I find the other indices of x which are also 22? In this example, I would expect a return of x(2) and x(8).

채택된 답변

Jan
Jan 2021년 3월 8일
x = [7, 22, 16, 20, 22, 3, 6, 22, 5]
index = 5;
a = x(5)
index = find(x == a)
% Or:
[UniqX, iX, iUniqX] = unique(x)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by