MATLAB: Possible to modify only one portion of an array in one line

arrayelementmodifymultiple conditions

I have large arrays (10 to 100k elements) and would like to change some values that are too high, but only after certain point. What I have in mind is something like this:
x = [100 80 70 20 40 10 50 1 60] ;
xMax = 30 ;
transition = 3 ;
x(x>xMax & x(i)>transition) = xMax
I would like to know if there is a more efficient and elegant way of doing it instead of with a loop, like this:
x = [100 80 70 20 40 10 50 1 60] ;
xMax = 30 ;
transition = 3 ;
for i = 1 :length(x)
if i > transition & x(i) > xMax
x(i) = xMax ;
end
end
Is it possible to include two conditions in this manner?
Thanks

Best Answer

x(x > xMax & 1:length(x) > transition) = xMax
Related Question