Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Very quick logical vector question

Subject: Very quick logical vector question

From: NouveauIX

Date: 16 Sep, 2009 19:40:18

Message: 1 of 13

Say I have a logical vector of [1,0,1,1,0]

How do I convert that to read out [true false true true false] in the command window?

Subject: Very quick logical vector question

From: arun

Date: 16 Sep, 2009 20:30:19

Message: 2 of 13

On Sep 16, 9:40 pm, NouveauIX <visua...@gmail.com> wrote:
> Say I have a logical vector of [1,0,1,1,0]
>
> How do I convert that to read out [true false true true false] in the command window?

Do you mean like this?
A = logical([1,0,1,1,0]);

B(A==1) = {'true'};
B(A==0) = {'false'};

best, arun.

Subject: Very quick logical vector question

From: Dave Brackett

Date: 16 Sep, 2009 20:31:01

Message: 3 of 13

NouveauIX <visualxd@gmail.com> wrote in message <2103580763.37217.1253130048353.JavaMail.root@gallium.mathforum.org>...
> Say I have a logical vector of [1,0,1,1,0]
>
> How do I convert that to read out [true false true true false] in the command window?

there's probably a much more straightforward way of doing it but this is one way providing you are happy with the output:

a=[1,0,1,1,0];

for i=1:length(a)
    if a(i)==1
        b{i}='true';
    else
        b{i}='false';
    end
end

c=b';
disp(c)

Subject: Very quick logical vector question

From: Matt Fig

Date: 16 Sep, 2009 20:36:19

Message: 4 of 13

One approach:

% Data
A = [1,0,1,1,0];

% Display
regexprep(sprintf('%i',A),{'1','0'},{' true ',' false '})

Subject: Very quick logical vector question

From: someone

Date: 16 Sep, 2009 20:40:25

Message: 5 of 13

NouveauIX <visualxd@gmail.com> wrote in message <2103580763.37217.1253130048353.JavaMail.root@gallium.mathforum.org>...
> Say I have a logical vector of [1,0,1,1,0]
>
> How do I convert that to read out [true false true true false] in the command window?

I doubt there is a way to do EXACTLY (display only) what you describe.

You could however, create a cell array the same size as above (initialized to all 'false' and set the logial ones index to 'true').

Subject: Very quick logical vector question

From: arun

Date: 16 Sep, 2009 20:45:17

Message: 6 of 13

On Sep 16, 10:31 pm, "Dave Brackett" <davebrack...@hotmail.com> wrote:
> NouveauIX <visua...@gmail.com> wrote in message <2103580763.37217.1253130048353.JavaMail.r...@gallium.mathforum.org>...
> > Say I have a logical vector of [1,0,1,1,0]
>
> > How do I convert that to read out [true false true true false] in the command window?
>
> there's probably a much more straightforward way of doing it but this is one way providing you are happy with the output:
>
> a=[1,0,1,1,0];
>
> for i=1:length(a)
>     if a(i)==1
>         b{i}='true';
>     else
>         b{i}='false';
>     end
> end
>
> c=b';
> disp(c)

For smaller size of 'a', for-loop seems to be faster (as usual).

a = logical(round(rand(100,1)));
tic;
for i=1:length(a)
    if a(i)==1
        b{i}='true';
    else
        b{i}='false';
    end
end
toc;
A = a;
tic;
B(A==1) = {'true'};
B(A==0) = {'false'};
toc;
% Elapsed time is 0.012134 seconds.
% Elapsed time is 0.024015 seconds.

when size(a) = 10000*1,
% Elapsed time is 0.218376 seconds.
% Elapsed time is 0.014273 seconds.

best, arun.

Subject: Very quick logical vector question

From: arun

Date: 16 Sep, 2009 20:55:45

Message: 7 of 13

On Sep 16, 10:36 pm, "Matt Fig" <spama...@yahoo.com> wrote:
> One approach:
>
> % Data
> A = [1,0,1,1,0];
>
> % Display
> regexprep(sprintf('%i',A),{'1','0'},{' true ',' false '})

wow, that's... compact!!!

Subject: Very quick logical vector question

From: Doug Schwarz

Date: 16 Sep, 2009 20:58:21

Message: 8 of 13

In article <h8rifp$i3f$1@fred.mathworks.com>,
 "someone" <someone@somewhere.net> wrote:

> NouveauIX <visualxd@gmail.com> wrote in message
> <2103580763.37217.1253130048353.JavaMail.root@gallium.mathforum.org>...
> > Say I have a logical vector of [1,0,1,1,0]
> >
> > How do I convert that to read out [true false true true false] in the
> > command window?
>
> I doubt there is a way to do EXACTLY (display only) what you describe.

Sure there is. You just have to overload the display function for
logical arguments. You can make it do anything you want.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Very quick logical vector question

From: Jos

Date: 16 Sep, 2009 20:59:03

Message: 9 of 13

arun <aragorn168b@gmail.com> wrote in message <22d710d1-68d0-488d-840b-0a378c6f4c82@z34g2000vbl.googlegroups.com>...
> On Sep 16, 10:31?pm, "Dave Brackett" <davebrack...@hotmail.com> wrote:
> > NouveauIX <visua...@gmail.com> wrote in message <2103580763.37217.1253130048353.JavaMail.r...@gallium.mathforum.org>...
> > > Say I have a logical vector of [1,0,1,1,0]
> >
> > > How do I convert that to read out [true false true true false] in the command window?
> >
> > there's probably a much more straightforward way of doing it but this is one way providing you are happy with the output:
> >
> > a=[1,0,1,1,0];
> >
> > for i=1:length(a)
> > ? ? if a(i)==1
> > ? ? ? ? b{i}='true';
> > ? ? else
> > ? ? ? ? b{i}='false';
> > ? ? end
> > end
> >
> > c=b';
> > disp(c)
>
> For smaller size of 'a', for-loop seems to be faster (as usual).
>
> a = logical(round(rand(100,1)));
> tic;
> for i=1:length(a)
> if a(i)==1
> b{i}='true';
> else
> b{i}='false';
> end
> end
> toc;
> A = a;
> tic;
> B(A==1) = {'true'};
> B(A==0) = {'false'};
> toc;
> % Elapsed time is 0.012134 seconds.
> % Elapsed time is 0.024015 seconds.
>
> when size(a) = 10000*1,
> % Elapsed time is 0.218376 seconds.
> % Elapsed time is 0.014273 seconds.
>
> best, arun.

You can improve both methods a lot by pre-allocating B!

Jos

Subject: Very quick logical vector question

From: NouveauIX

Date: 17 Sep, 2009 00:32:38

Message: 10 of 13

Thanks a lot guys!

Subject: Very quick logical vector question

From: avdhesh ahirwar

Date: 17 Sep, 2009 07:02:04

Message: 11 of 13

NouveauIX <visualxd@gmail.com> wrote in message <121681881.38304.1253147588525.JavaMail.root@gallium.mathforum.org>...
> Thanks a lot guys!

Subject: Very quick logical vector question

From: Bruno Luong

Date: 17 Sep, 2009 07:13:01

Message: 12 of 13

"avdhesh ahirwar" <elect_final@yaoo.co.in> wrote in message <h8smtc$9o3$1@fred.mathworks.com>...
> NouveauIX <visualxd@gmail.com> wrote in message <121681881.38304.1253147588525.JavaMail.root@gallium.mathforum.org>...
> > Thanks a lot guys!

Dirty way of Matt's solution, just for the sake of getting an even shorter command:

% warning off
strrep(strrep(A,0,'false '),1,'true ')

% Or more visible
strrep(strrep(A,false,'false '),true,'true ')

Bruno

Subject: Very quick logical vector question

From: Steven Lord

Date: 17 Sep, 2009 13:38:44

Message: 13 of 13


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
news:h8snht$rud$1@fred.mathworks.com...
> "avdhesh ahirwar" <elect_final@yaoo.co.in> wrote in message
> <h8smtc$9o3$1@fred.mathworks.com>...
>> NouveauIX <visualxd@gmail.com> wrote in message
>> <121681881.38304.1253147588525.JavaMail.root@gallium.mathforum.org>...
>> > Thanks a lot guys!
>
> Dirty way of Matt's solution, just for the sake of getting an even shorter
> command:
>
> % warning off
> strrep(strrep(A,0,'false '),1,'true ')
>
> % Or more visible
> strrep(strrep(A,false,'false '),true,'true ')

A = [1 0 0 1 0];
B = {'false', 'true'};
C = B(A+1)
% or
mat2str(logical(A))
% or
fprintf('%s ', B{A+1})

The last one won't exactly reproduce A if A is a matrix, but I'm including
it because it could be useful.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us