COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Logical Vectors From Values

• Email
• 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

See related articles to this posting

```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

```x = [3 5 8];
y = zeros(1, max(x));
y(x) = 1
```
 0

```Oops I didn't look at the subject line, just the example. Walter's answer is the right one, ignore mine.
```
 0

```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

```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

```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

```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
>> help true

>>

:(

--
```
 0

```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

```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

```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
>  >> help true
>
>
>  >>
>
> :(
>
> --

Of course, you could fix that!

function T = true(N)
% Hacky-TRUE
T = ~zeros(N);
```
 0

```"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

```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
>  >> help true
>

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

```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
> >  >> help true
> >
>
> 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

```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
> >  >> help true
> >
>
> 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

```Very nice, Oleg.  Silly solutions are a lot more fun.
```
 0

```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

```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
>>  >> help true
>>
>>
>>  >>
>>
>> :(
>>
>> --
>
>
> Of course, you could fix that!
>
> function T = true(N)
> % Hacky-TRUE
> T = ~zeros(N);

true(aye-that);

--
```
 0

```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

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

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

us
```
 0

```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

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

Not even a snicker???  :)

--
```
 0

```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

```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

```"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

```"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

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

```
 0