Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
dpb

Posts: 8,296
Registered: 6/7/07
Re: unique - logical indexing
Posted: Jan 22, 2014 5:46 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic 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.

--


--




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.