### Tags: 9basically, array, arraya0, consider, ends, following, matlab, programming, togetb, truncate

# How to truncate an array

On Programmer » Matlab

2,564 words with 2 Comments; publish: Tue, 06 May 2008 21:49:00 GMT; (20044.92, « »)

Consider the following array:

a=[0.1 0.1 0.2 0.9 1 1 1]

I want to find a quick way to truncate this array at the two ends to

get:

b=[ 0.2 0.9]

Basically, I want to truncate the elements of "a" that are smaller

than 0.2 and greater than 0.9. Also, there is an indice matrice c=[1

2 3 4 5 6 7]. I want to truncate this indice matrice according to the

elements removed from a.

I will appreciate any help.

Thank you.

*http://matlab.todaysummary.com/q_matlab_28871.html*

All Comments

Leave a comment...

- 2 Comments
- In article <ef19f2b.-1.matlab.todaysummary.com.webx.raydaftYaTP>, Onur <onuruncu.matlab.todaysummary.com.yahoo.com> wrote:
> Consider the following array:

> a=[0.1 0.1 0.2 0.9 1 1 1]

> I want to find a quick way to truncate this array at the two ends to

> get:

> b=[ 0.2 0.9]

> Basically, I want to truncate the elements of "a" that are smaller

> than 0.2 and greater than 0.9. Also, there is an indice matrice c=[1

> 2 3 4 5 6 7]. I want to truncate this indice matrice according to the

> elements removed from a.

> I will appreciate any help.

> Thank you.

--

Hello Onur,

I assume from your example that the elements of vector 'a' are always

monotonically non-decreasing. If so, you can do this:

i = find((b(1)<=a)&(a<=b(2)));

a = a(i);

c = c(i);

If 'a' isn't monotone, this may eliminate elements of 'a' other than at

its two ends. In that case you should be more specific as to what you

wish to happen.

(Remove "xyzzy" and ".invalid" to send me email.)

Roger Stafford

#1; Tue, 06 May 2008 21:50:00 GMT

- In article <ef19f2b.-1.matlab.todaysummary.com.webx.raydaftYaTP>, Onur <onuruncu.matlab.todaysummary.com.yahoo.com> wrote:
- Thanks Roger!
Yes, my data is monotonous. This absolutely solves my problem.

Thanks again.

~Onur.

Roger Stafford wrote:

>

> In article <ef19f2b.-1.matlab.todaysummary.com.webx.raydaftYaTP>, Onur

<onuruncu.matlab.todaysummary.com.yahoo.com>

> wrote:

>

ends

> to

> smaller

> c=[1

to

> the

> --

> Hello Onur,

> I assume from your example that the elements of vector 'a' are

> always

> monotonically non-decreasing. If so, you can do this:

> i = find((b(1)<=a)&(a<=b(2)));

> a = a(i);

> c = c(i);

> If 'a' isn't monotone, this may eliminate elements of 'a' other

> than at

> its two ends. In that case you should be more specific as to what

> you

> wish to happen.

> (Remove "xyzzy" and ".invalid" to send me email.)

> Roger Stafford

>

#2; Tue, 06 May 2008 21:51:00 GMT

- Thanks Roger!