user1218247
user1218247

Reputation:

Constrain values in matlab

If i have a simple array in matlab, say:

a = [0 1 2 3 4 5 6 0 0]

How do i constrain the values in that array (if for example i want the maximum value to be 1, so to get to:

a = [0 1 1 1 1 1 1 0 0]

What would be the simplest, most efficient way to do that?

Upvotes: 3

Views: 1644

Answers (3)

tmpearce
tmpearce

Reputation: 12693

a(a>1) = 1;

This would do what you're asking... you can follow the same pattern for other constraints.

Edit: commenter is correct, fixed.

Upvotes: 6

macduff
macduff

Reputation: 4685

Try:

arrayfun(@(x) min(1,x),a)

For the max value and substitute max(val,x) for the min.

Upvotes: 1

Fred Foo
Fred Foo

Reputation: 363807

a > 0

or

min(a, 1)

(Tested in Octave.)

Upvotes: 3

Related Questions