Logical Vectors From Values

  • Follow


Dear All,  Is there a neat way of getting from, say,

[3, 5, 8]

to

[0, 0, 1, 0, 1, 0, 0, 1]?
0
Reply james 3/30/2010 8:57:05 PM

james bejon wrote:
> Dear All,  Is there a neat way of getting from, say,
> 
> [3, 5, 8]
> 
> to
> 
> [0, 0, 1, 0, 1, 0, 0, 1]?

B = false(1,max(A));
B(A) = true;
0
Reply Walter 3/30/2010 9:07:45 PM


x = [3 5 8];
y = zeros(1, max(x));
y(x) = 1
0
Reply David 3/30/2010 9:09:06 PM

Oops I didn't look at the subject line, just the example. Walter's answer is the right one, ignore mine.
0
Reply David 3/30/2010 9:12:05 PM

Walter Roberson wrote:
> james bejon wrote:
>> Dear All,  Is there a neat way of getting from, say,
>>
>> [3, 5, 8]
>>
>> to
>>
>> [0, 0, 1, 0, 1, 0, 0, 1]?
> 
> B = false(1,max(A));
> B(A) = true;

Additional note: if there can be big gaps between the true values, then you 
may wish to consider using sparse arrays. Sparse arrays are supported for 
values of class double, and values of class logical, but (not as yet) for any 
other class.
0
Reply Walter 3/30/2010 9:20:47 PM

james bejon wrote:
> Dear All,  Is there a neat way of getting from, say,
> 
> [3, 5, 8]
> 
> to
> 
> [0, 0, 1, 0, 1, 0, 0, 1]?

Alternately,

o(x)=1;          % create the 0/1 vector
l = logical(o);  % turn it into a logical one

Too bad for things like this ML doesn't support nested assignment..

l = logical(o(x)=1);   % Doesn't work, of course... :(

--

0
Reply dpb 3/30/2010 9:37:39 PM

On Mar 30, 2:37=A0pm, dpb <n...@non.net> wrote:
> james bejon wrote:
> > Dear All, =A0Is there a neat way of getting from, say,
>
> > [3, 5, 8]
>
> > to
>
> > [0, 0, 1, 0, 1, 0, 0, 1]?
>
> Alternately,
>
> o(x)=3D1; =A0 =A0 =A0 =A0 =A0% create the 0/1 vector
> l =3D logical(o); =A0% turn it into a logical one
>
> Too bad for things like this ML doesn't support nested assignment..
>
> l =3D logical(o(x)=3D1); =A0 % Doesn't work, of course... :(
>
> --

If you were going to do that, why not just make it logical in the
first place?

o(x) =3D true;

-Nathan
0
Reply Nathan 3/30/2010 9:43:30 PM

Nathan wrote:
....

> If you were going to do that, why not just make it logical in the
> first place?
> 
> o(x) = true;

Primarily because...

 >> which true
true not found.
 >> help true

true.m not found.

 >>

:(

--
0
Reply dpb 3/30/2010 9:51:56 PM

dpb wrote:
> james bejon wrote:
>> Dear All,  Is there a neat way of getting from, say,
>>
>> [3, 5, 8]
>>
>> to
>>
>> [0, 0, 1, 0, 1, 0, 0, 1]?
> 
> Alternately,
> 
> o(x)=1;          % create the 0/1 vector
> l = logical(o);  % turn it into a logical one
> 
> Too bad for things like this ML doesn't support nested assignment..
> 
> l = logical(o(x)=1);   % Doesn't work, of course... :(

Well, if you are going to be like that!

l = any(bsxfun(@eq,1:max(x),x.'));
0
Reply Walter 3/30/2010 9:54:47 PM

Thanks for all the really helpful replies!

I'm currently toying with the (perhaps hungry?) expression:

ismember((1:A(end)), A)

(I can assume A is in ascending order)
0
Reply james 3/30/2010 10:08:05 PM

dpb <none@non.net> wrote in message <hotrs9$qbg$1@news.eternal-september.org>...
> Nathan wrote:
> ...
> 
> > If you were going to do that, why not just make it logical in the
> > first place?
> > 
> > o(x) = true;
> 
> Primarily because...
> 
>  >> which true
> true not found.
>  >> help true
> 
> true.m not found.
> 
>  >>
> 
> :(
> 
> --


Of course, you could fix that!

function T = true(N)
% Hacky-TRUE
T = ~zeros(N);
0
Reply Matt 3/30/2010 10:10:22 PM

"james bejon" <jamesbejon@yahoo.co.uk> wrote in message <hotso5$2ib$1@fred.mathworks.com>...
> Thanks for all the really helpful replies!
> 
> I'm currently toying with the (perhaps hungry?) expression:
> 
> ismember((1:A(end)), A)
> 
> (I can assume A is in ascending order)


That works, but it will be slower and less readable than many of the other solutions.
0
Reply Matt 3/30/2010 10:11:04 PM

dpb wrote:
> Nathan wrote:
> ...
> 
>> If you were going to do that, why not just make it logical in the
>> first place?
>>
>> o(x) = true;
> 
> Primarily because...
> 
>  >> which true
> true not found.
>  >> help true
> 
> true.m not found.

But if you have logical() at your disposal, then you could use

o(x) = logical(1);


So now who is going to give us an answer in terms of logical(accumarray(...)) 
?  ;-)

0
Reply Walter 3/30/2010 10:14:34 PM

Walter Roberson
> dpb wrote:
> > Nathan wrote:
> > ...
> > 
> >> If you were going to do that, why not just make it logical in the
> >> first place?
> >>
> >> o(x) = true;
> > 
> > Primarily because...
> > 
> >  >> which true
> > true not found.
> >  >> help true
> > 
> > true.m not found.
> 
> But if you have logical() at your disposal, then you could use
> 
> o(x) = logical(1);
> 
> 
> So now who is going to give us an answer in terms of logical(accumarray(...)) 
> ?  ;-)

Out = accumarray(A.',true,[],@logical);

Another versione :). Only if unique(A) == A.
0
Reply Oleg 3/30/2010 11:04:06 PM

Walter Roberson
> dpb wrote:
> > Nathan wrote:
> > ...
> > 
> >> If you were going to do that, why not just make it logical in the
> >> first place?
> >>
> >> o(x) = true;
> > 
> > Primarily because...
> > 
> >  >> which true
> > true not found.
> >  >> help true
> > 
> > true.m not found.
> 
> But if you have logical() at your disposal, then you could use
> 
> o(x) = logical(1);
> 
> 
> So now who is going to give us an answer in terms of logical(accumarray(...)) 
> ?  ;-)

Out = accumarray(A.',true,[],@logical);

Another versione :). Only if unique(A) == A.
0
Reply Oleg 3/30/2010 11:05:23 PM

Very nice, Oleg.  Silly solutions are a lot more fun.
0
Reply james 3/30/2010 11:40:26 PM

Oleg Komarov wrote:
....

>> But if you have logical() at your disposal, then you could use
>>
>> o(x) = logical(1);
>>
....

Indeedy, do...very good (and why didn't _I_ think of it??? :( )...  :)

--
0
Reply dpb 3/30/2010 11:41:08 PM

Matt Fig wrote:
> dpb <none@non.net> wrote in message 
> <hotrs9$qbg$1@news.eternal-september.org>...
>> Nathan wrote:
>> ...
>>
>> > If you were going to do that, why not just make it logical in the
>> > first place?
>> > > o(x) = true;
>>
>> Primarily because...
>>
>>  >> which true
>> true not found.
>>  >> help true
>>
>> true.m not found.
>>
>>  >>
>>
>> :(
>>
>> -- 
> 
> 
> Of course, you could fix that!
> 
> function T = true(N)
> % Hacky-TRUE
> T = ~zeros(N);

true(aye-that);

--
0
Reply dpb 3/30/2010 11:42:16 PM

Walter Roberson wrote:
> dpb wrote:
>> james bejon wrote:
>>> Dear All,  Is there a neat way of getting from, say,
>>>
>>> [3, 5, 8]
>>>
>>> to
>>>
>>> [0, 0, 1, 0, 1, 0, 0, 1]?
>>
>> Alternately,
>>
>> o(x)=1;          % create the 0/1 vector
>> l = logical(o);  % turn it into a logical one
>>
>> Too bad for things like this ML doesn't support nested assignment..
>>
>> l = logical(o(x)=1);   % Doesn't work, of course... :(
> 
> Well, if you are going to be like that!
> 
> l = any(bsxfun(@eq,1:max(x),x.'));

Chuckle...

--
0
Reply dpb 3/30/2010 11:43:56 PM

dpb
> > l = any(bsxfun(@eq,1:max(x),x.'));
> Chuckle...

this is NOT funny - this is sheer, cruel abuse of ML syntax...

us
0
Reply us 3/30/2010 11:54:06 PM

Oleg Komarov wrote:

>> So now who is going to give us an answer in terms of 
>> logical(accumarray(...)) ?  ;-)
> 
> Out = accumarray(A.',true,[],@logical);
> 
> Another versione :). Only if unique(A) == A.

Out = logical(accumarray(A.', 1));

This version does not require unique(A) == A.

I am not at all experienced in accumarray, but looking at the 
documentation, it appears to me that your version would also work with 
repeated elements if you changed the @logical to @any .
0
Reply Walter 3/31/2010 3:09:41 AM

us wrote:
> dpb
>> > l = any(bsxfun(@eq,1:max(x),x.'));
>> Chuckle...
> 
> this is NOT funny - ...

Not even a snicker???  :)

--
0
Reply dpb 3/31/2010 4:45:38 AM

There's some really neat stuff here.  Thanks so much.  I'm very new to Matlab, but am starting to see how concise its code can be.  On a related note, Can anyone think of a neat way of converting, say,

[3, 8, 12]

to

[1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3]?

Or better still:

[3, 8, 12;
 3, 7, 12]

to

[1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3;
 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]?
0
Reply james 3/31/2010 7:16:05 AM

Walter Roberson <roberson@hushmail.com> wrote in message <houedm$r10$1@canopus.cc.umanitoba.ca>...
> Oleg Komarov wrote:
> 
> >> So now who is going to give us an answer in terms of 
> >> logical(accumarray(...)) ?  ;-)
> > 
> > Out = accumarray(A.',true,[],@logical);
> > 
> > Another versione :). Only if unique(A) == A.
> 
> Out = logical(accumarray(A.', 1));
> 
> This version does not require unique(A) == A.
> 
> I am not at all experienced in accumarray, but looking at the 
> documentation, it appears to me that your version would also work with 
> repeated elements if you changed the @logical to @any .

You're right!

Oleg
0
Reply Oleg 3/31/2010 7:23:06 AM

"james bejon" 
> There's some really neat stuff here.  Thanks so much.  I'm very new to Matlab, but am starting to see how concise its code can be.  On a related note, Can anyone think of a neat way of converting, say,
> 
> [3, 8, 12]
> 
> to
> 
> [1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3]?
> 
> Or better still:
> 
> [3, 8, 12;
>  3, 7, 12]
> 
> to
> 
> [1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3;
>  1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]?

Be rude!

A = [3,8,12];
rude(A./(1:numel(A)), 1:numel(A))  % FEX by us

Oleg
0
Reply Oleg 3/31/2010 7:33:04 AM

"james bejon" <jamesbejon@yahoo.co.uk> wrote in message <housrl$1vl$1@fred.mathworks.com>...
> There's some really neat stuff here.  Thanks so much.  I'm very new to Matlab, but am starting to see how concise its code can be.  On a related note, Can anyone think of a neat way of converting, say,
> 
> [3, 8, 12]
> 
> to
> 
> [1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3]?
> 
> Or better still:
> 
> [3, 8, 12;
>  3, 7, 12]
> 
> to
> 
> [1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3;
>  1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]?

c=[3, 8, 12;
   3, 7, 12]

r = repmat((1:size(c,1)).',[1 size(c,2)]);
A = accumarray([r(:) c(:)+1],1);
A(:,1) = 1;
A = cumsum(A,2);
A(:,end) = [];

disp(A)

% Bruno
0
Reply Bruno 3/31/2010 9:59:06 AM

Hi Bruno.  Thanks very much for your suggestion.  Works perfectly.  I like your idea with the cumsum function.  In the end, I've gone for something like (I happen to know that all the rows are of length 25)

c = transpose([3, 11, 23, 24; 3, 10, 23, 24]) + 1;
z = zeros(25, size(c, 2));
n = repmat(1:size(c, 2), size(c, 1), 1);
z(sub2ind(size(z), c, n)) = 1;
z = cumsum(z, 1) + 1;

P.S.  I nicked the sub2ind idea from your following RANK thread!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/163003#731927
0
Reply james 4/1/2010 12:15:22 AM

26 Replies
132 Views

(page loaded in 0.11 seconds)

Similiar Articles:


















7/14/2012 10:43:36 PM


Reply: