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:
Finding 0's and setting to 1

Subject: Finding 0's and setting to 1

From: Ulrik Nash

Date: 3 Sep, 2011 11:09:10

Message: 1 of 9

Hi Everyone,

Suppose I have a vector A:

A = [1 0 0 1]

and if wish to set all 0's in A to 1 to create B:

B = [1 1 1 1]

How can I do this?

Regards,

Ulrik.

Subject: Finding 0's and setting to 1

From: Wayne King

Date: 3 Sep, 2011 12:20:28

Message: 2 of 9

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <j3t1sm$dtk$1@newscl01ah.mathworks.com>...
> Hi Everyone,
>
> Suppose I have a vector A:
>
> A = [1 0 0 1]
>
> and if wish to set all 0's in A to 1 to create B:
>
> B = [1 1 1 1]
>
> How can I do this?
>
> Regards,
>
> Ulrik.

Hi Ulrik, one way:

A(A<1)=1;

Wayne

Subject: Finding 0's and setting to 1

From: Nasser M. Abbasi

Date: 3 Sep, 2011 12:22:05

Message: 3 of 9

On 9/3/2011 4:09 AM, Ulrik Nash wrote:
> Hi Everyone,
>
> Suppose I have a vector A:
>
> A = [1 0 0 1]
>
> and if wish to set all 0's in A to 1 to create B:
>
> B = [1 1 1 1]
>
> How can I do this?
>
> Regards,
>
> Ulrik.

EDU>> A(A==0)=1

A =

      1 1 1 1

--Nasser

Subject: Finding 0's and setting to 1

From: Ulrik Nash

Date: 3 Sep, 2011 12:37:13

Message: 4 of 9

"Nasser M. Abbasi" <nma@12000.org> wrote in message <j3t66h$tjg$1@speranza.aioe.org>...
> On 9/3/2011 4:09 AM, Ulrik Nash wrote:
> > Hi Everyone,
> >
> > Suppose I have a vector A:
> >
> > A = [1 0 0 1]
> >
> > and if wish to set all 0's in A to 1 to create B:
> >
> > B = [1 1 1 1]
> >
> > How can I do this?
> >
> > Regards,
> >
> > Ulrik.
>
> EDU>> A(A==0)=1
>

Thank you both, so simple...

> A =
>
> 1 1 1 1
>
> --Nasser

Subject: Finding 0's and setting to 1

From: dpb

Date: 3 Sep, 2011 13:41:26

Message: 5 of 9

On 9/3/2011 7:37 AM, Ulrik Nash wrote:
> "Nasser M. Abbasi" <nma@12000.org> wrote in message
> <j3t66h$tjg$1@speranza.aioe.org>...
>> On 9/3/2011 4:09 AM, Ulrik Nash wrote:
>> > Hi Everyone,
>> >
>> > Suppose I have a vector A:
>> >
>> > A = [1 0 0 1]
>> >
>> > and if wish to set all 0's in A to 1 to create B:
>> >
>> > B = [1 1 1 1]
>> >
>> > How can I do this?
>> >
>> > Regards,
>> >
>> > Ulrik.
>>
>> EDU>> A(A==0)=1
>>
>
> Thank you both, so simple...
>
>> A =
>>
>> 1 1 1 1

For the actual problem as given

B=ones(size(A));

is possibly quicker for large A eliminating the logical.

--

Subject: Finding 0's and setting to 1

From: Nasser M. Abbasi

Date: 3 Sep, 2011 14:05:54

Message: 6 of 9

On 9/3/2011 6:41 AM, dpb wrote:
> On 9/3/2011 7:37 AM, Ulrik Nash wrote:
>> "Nasser M. Abbasi"<nma@12000.org> wrote in message
>> <j3t66h$tjg$1@speranza.aioe.org>...
>>> On 9/3/2011 4:09 AM, Ulrik Nash wrote:
>>>> Hi Everyone,
>>>>
>>>> Suppose I have a vector A:
>>>>
>>>> A = [1 0 0 1]
>>>>
>>>> and if wish to set all 0's in A to 1 to create B:
>>>>
>>>> B = [1 1 1 1]
>>>>
>>>> How can I do this?
>>>>
>>>> Regards,
>>>>
>>>> Ulrik.
>>>
>>> EDU>> A(A==0)=1
>>>
>>
>> Thank you both, so simple...
>>
>>> A =
>>>
>>> 1 1 1 1
>
> For the actual problem as given
>
> B=ones(size(A));
>
> is possibly quicker for large A eliminating the logical.
>
> --

yes ofcourse, but I assumed there might be other values in there besides
'1' and '0' when OP said "wish to set all 0's in A to 1".

But now you mention it, if all was needed is a vector of one's, then
your solution is the right one ofcourse.

--Nasser

Subject: Finding 0's and setting to 1

From: dpb

Date: 3 Sep, 2011 17:57:01

Message: 7 of 9

On 9/3/2011 9:05 AM, Nasser M. Abbasi wrote:
...

>
> yes ofcourse, but I assumed there might be other values in there besides
> '1' and '0' when OP said "wish to set all 0's in A to 1".
>
> But now you mention it, if all was needed is a vector of one's, then
> your solution is the right one ofcourse.
...

Sure, but also just pointing out to OP the answer to the question as
posed in the example...OP seems to be doing very little (if any)
thinking here in the myriad of almost identical problem postings, I'm
gently trying to point out a just a _little_ thought (or gasp!! reading
"Getting Started") might be faster than waiting on cssm. :(

--

Subject: Finding 0's and setting to 1

From: Ulrik Nash

Date: 3 Sep, 2011 21:03:07

Message: 8 of 9

dpb <none@non.net> wrote in message <j3tppo$jov$1@speranza.aioe.org>...
> On 9/3/2011 9:05 AM, Nasser M. Abbasi wrote:
> ...
>
> >
> > yes ofcourse, but I assumed there might be other values in there besides
> > '1' and '0' when OP said "wish to set all 0's in A to 1".
> >
> > But now you mention it, if all was needed is a vector of one's, then
> > your solution is the right one ofcourse.
> ...
>
> Sure, but also just pointing out to OP the answer to the question as
> posed in the example...OP seems to be doing very little (if any)
> thinking here in the myriad of almost identical problem postings, I'm
> gently trying to point out a just a _little_ thought (or gasp!! reading
> "Getting Started") might be faster than waiting on cssm. :(
>
> --
I see your point dpb. However, my questions do contribute to both the body of knowledge available to others here, and my own learning.... Still, you have a point.

Subject: Finding 0's and setting to 1

From: dpb

Date: 3 Sep, 2011 23:29:30

Message: 9 of 9

On 9/3/2011 4:03 PM, Ulrik Nash wrote:
...

> I see your point dpb. However, my questions do contribute to both the
> body of knowledge available to others here, and my own learning....
> Still, you have a point.

Well, they stretch that body _very_ little, indeed...

--

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