MATLAB: Sort a specific part of a row

descendingmatrixsort

what if i have a row [ 2 3 7 1 4] and i want to sort the values that are after the 4 only descendingly.
P.S. i want it generic not on this case only.
Thank you

Best Answer

Your question could have two meanings:
1)
NewVector = [YourVector(1:4), sort(YourVector(5:end), 'descend')];
2)
idx = find(YourVector == 4, 1, 'first');
if ~isempty(idx)
NewVector = [YourVector(1:idx), sort(YourVector(idx+1:end), 'descend')];
else
NewVector = YourVector;
end