How to find the indices of element occuring once in a vector?

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

 採用された回答

Cedric
Cedric 2014 年 5 月 23 日
編集済み: Cedric 2014 年 5 月 23 日

2 投票

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) ;

4 件のコメント

Sameer
Sameer 2014 年 5 月 23 日
Hi...Thanks a lot for replying....its able to find the correct value. And to solve my purpose I just have to add a line which was suggested by mahdi. Thanks a lot...:)
Regards
Cedric
Cedric 2014 年 5 月 23 日
Note that you can contract this into a 2 liners
pos = find( A == (-1 + 2 * (sum(A==1) == 1)) ) ;
value = A(pos) ;
but you lose in clarity.
Mahdi
Mahdi 2014 年 5 月 23 日
Very nice Cedric! I didn't even think of doing that way!
Cedric
Cedric 2014 年 5 月 23 日
編集済み: Cedric 2014 年 5 月 23 日
Well, I would personally go for clarity .. otherwise there is even a one liner actually:
[~,pos,value] = find( A .* (A == -1 + 2*(sum(A==1)==1)) )

サインインしてコメントする。

その他の回答 (3 件)

George Papazafeiropoulos
George Papazafeiropoulos 2014 年 5 月 23 日
編集済み: George Papazafeiropoulos 2014 年 5 月 23 日

3 投票

A=[1 1 0 -1 0 0 1 0 1 1];
[~,c]=histc(A,unique(A));
out=A(c==1);

2 件のコメント

Sameer
Sameer 2014 年 5 月 23 日
Hi...Thanks for replying.
I want to know the index of an entry which is unique to a particulat set of values. lets say
if A=[1 1 1 -1 0 0] then I want the indices of -1. if A=[-1 -1 -1 0 1 0] then I want the indices of 1.
I hope I made it clear what I am looking for.
Awaiting your Reply.
Regards
Cedric
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.

サインインしてコメントする。

Mahdi
Mahdi 2014 年 5 月 23 日

1 投票

If you're looking specifically for the value of -1, you can use the following:
index1=find(A==-1)

3 件のコメント

Sameer
Sameer 2014 年 5 月 23 日
Hi...Thanks for replying.
I want to know the index of an entry which is unique to a particulat set of values. lets say
if A=[1 1 1 -1 0 0] then I want the indices of -1. if A=[-1 -1 -1 0 1 0] then I want the indices of 1.
I hope I made it clear what I am looking for.
Awaiting your Reply.
Regards
Mahdi
Mahdi 2014 年 5 月 23 日
You can use the unique function.
[C, ia, ic]=unique(A)
Where the matrix C gives the unique value (1 or -1 in this case), and ia gives the indices where these are found.
Sameer
Sameer 2014 年 5 月 23 日
Thank you....but unique command returns the values that are present in the vector so here it is -1 0 1. But I am looking for the single value that is either 1 or -1 and then the indices of that particular value.
Regards

サインインしてコメントする。

George Papazafeiropoulos
George Papazafeiropoulos 2014 年 5 月 23 日
編集済み: George Papazafeiropoulos 2014 年 5 月 23 日

0 投票

A=[1 1 -1 0 0 0 1 0 1 1];
[~,c]=histc(A,unique(A));
out=find(c==1);

3 件のコメント

Sameer
Sameer 2014 年 5 月 23 日
Thank you...I appreciate your help. But the above solution is confined to a particular A(i guess) and its returing the indicies of only -1 if the elements of A changes.
Actually I have large number of different vectors and in each either 1 or -1 is unique (either only 1 or -1 in a vector like A=[1,1,1,-1,0,0] (-1 is occuring once),B=[-1,-1,-1,1,0,0](1 is occuring once)) so for A i should have the indices of -1 for B indices of 1. so I am looking for a code which will determine the unique number in a vector and return its corresponding indices and only of that particular number...not like unique function.
Awaiting your reply.
Regards
George Papazafeiropoulos
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...
Sameer
Sameer 2014 年 5 月 23 日
編集済み: Sameer 2014 年 5 月 23 日
Unfortunately its not working as in the attached image you can see that 1 is the unique and its index should be 7 but the code is showing for -1 instead that is 1 2 3. where A=[-1 -1 -1 0 0 0 1 0].
Awaiting your response.
Regards

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

タグ

質問済み:

2014 年 5 月 23 日

編集済み:

2014 年 5 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by