How to find the indices of element occuring once in a vector?
조회 수: 5 (최근 30일)
이전 댓글 표시
Hello all
I want to know...How can I get the indices of a value that is occuring only once in a vector...please guide.
Example: A=[1 1 0 -1 0 0 1 0 1 1]
Task: To identify the indices of -1 (as it is occuring only once) in A.
Please Help!!!
Regards
댓글 수: 0
채택된 답변
Cedric
2014년 5월 23일
편집: Cedric
2014년 5월 23일
There are ways to solve your problem based on HISTC or ACCUMARRAY. However, the simplest approach if you really have only two situations (unique 1 or unique -1) is probably the following:
if sum( A == 1 ) == 1
pos = find( A == 1 ) ;
else
pos = find( A == -1 ) ;
end
value = A(pos) ;
추가 답변 (3개)
George Papazafeiropoulos
2014년 5월 23일
편집: George Papazafeiropoulos
2014년 5월 23일
A=[1 1 0 -1 0 0 1 0 1 1];
[~,c]=histc(A,unique(A));
out=A(c==1);
댓글 수: 2
Cedric
2014년 5월 23일
Sagar, you should take the time to understand his example. In particular, see what c is, what c==1 is, etc. Maybe read about logical indexing, and if you cannot use the latter and really need the position of unique element(s), read about FIND.
George Papazafeiropoulos
2014년 5월 23일
편집: George Papazafeiropoulos
2014년 5월 23일
A=[1 1 -1 0 0 0 1 0 1 1];
[~,c]=histc(A,unique(A));
out=find(c==1);
댓글 수: 3
George Papazafeiropoulos
2014년 5월 23일
Try the above code for different A. Define A as you want and then execute the two last lines of the code. I think it works...
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!