Reputation:
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
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
Reputation: 4685
Try:
arrayfun(@(x) min(1,x),a)
For the max value and substitute max(val,x) for the min.
Upvotes: 1