Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: unique - logical indexing
Replies: 2   Last Post: Jan 22, 2014 5:46 PM

 Search Thread: Advanced Search

 Messages: [ Previous | Next ]
 dpb Posts: 9,850 Registered: 6/7/07
Re: unique - logical indexing
Posted: Jan 22, 2014 5:46 PM
 Plain Text Reply

On 1/22/2014 2:56 PM, mklcst mklcst wrote:
> "mklcst mklcst" wrote in message <lbpaoc\$30p\$1@newscl01ah.mathworks.com>...
>> Hi,
>> suppose I have this column vector: A = [1 2 3 3 4 5 6 7 7 8 9]'
>>
>> If I use unique I get:
>>
>> [C, ia] = unique(A);
>>
>> where ia is a column vector of indices C = A(ia).
>>
>> Therefore: ia = [1 2 3 5 6 7 8 10 11]';
>>
>> I need is as a logical vector:
>>
>> ia = [1 1 1 0 1 1 1 0 1 1];
>>
>> There is a quick way to do it?

>
>
> I did in this way but I would like to know if it exists a better way:
>
> [~,ia] = unique(A);
> isun2 = zeros(length(A),1);
> isun2(isun) = 1; isun2 = logical(isun2);

Slightly...instead of logical() build the logical vector directly.

isun2=false(size(A));
isun2(ia)=true;

NB: Current default behavior of unique() is to return ia

ia = [1 2 4 5 6 7 9 10 11]

though, instead of [1 2 3 5 ...]. The last repeated value instead of
first. If this is important to your application, you've a problem
going forward. The newer versions have a new optional argument 'legacy'
to preserve the previous behavior which is good if you remember to make
the correction but, of course, your current version won't know what to
do with it if you try to be proactive. So, even trying to be good and
build in compatibility breaks it...TMW can't win for losing.

--

--

Date Subject Author
1/22/14 mklcst mklcst
1/22/14 mklcst mklcst
1/22/14 dpb

© The Math Forum at NCTM 1994-2018. All Rights Reserved.