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:
Changing values in array if they meet certain critria

Subject: Changing values in array if they meet certain critria

From: Ulrik Nash

Date: 17 Dec, 2009 12:49:06

Message: 1 of 3

Hi everyone,

I have an array A with many numbers:

12
32
35
45
...
nth

Now I wish to change the numbers in the array, if they meet certain criteria, for example, if they are larger than 30, say. How do I do this without using a for loop?

Regards,

Ulrik.

Subject: Changing values in array if they meet certain critria

From: Jos (10584)

Date: 17 Dec, 2009 13:07:06

Message: 2 of 3

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hgd9c2$et9$1@fred.mathworks.com>...
> Hi everyone,
>
> I have an array A with many numbers:
>
> 12
> 32
> 35
> 45
> ...
> nth
>
> Now I wish to change the numbers in the array, if they meet certain criteria, for example, if they are larger than 30, say. How do I do this without using a for loop?
>
> Regards,
>
> Ulrik.

Use one of the powers of matlab: logical indexing

a = [1 12 3 17 2 3 14 21]
q = a > 10 % a logical vector (true or false)
a(q) = a(q) + 50

hth
Jos

Subject: Changing values in array if they meet certain critria

From: Engineering

Date: 17 Dec, 2009 13:21:09

Message: 3 of 3

On Dec 17, 6:07 pm, "Jos (10584) " <#10...@fileexchange.com> wrote:
> "Ulrik Nash" <u...@sam.sdu.dk> wrote in message <hgd9c2$et...@fred.mathworks.com>...
> > Hi everyone,
>
> > I have an array A with many numbers:
>
> > 12
> > 32
> > 35
> > 45
> > ...
> > nth
>
> > Now I wish to change the numbers in the array, if they meet certain criteria, for example, if they are larger than 30, say. How do I do this without using a for loop?
>
> > Regards,
>
> > Ulrik.
>
> Use one of the powers of matlab: logical indexing
>
> a = [1 12 3 17 2 3 14 21]
> q = a > 10 % a logical vector (true or false)
> a(q) = a(q) + 50
>
> hth
> Jos
Go with this link for answer
http://groups.google.com/group/engineering-works/web/engineering-works

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