MATLAB: Return Matrix to Original Order

findindicesordersortsortrowsvector

Hi all,
I have a column vector RPM_c, where I want to split it to elements below and above the vector's mean value, perform a different operation in the two respective groups of elements, and place them back to their original position as found RPM_c.
I know how to do everything, except for the last part of placing them in their original position. For example:
RPM_c = [m x 1] ; %an m x 1 matrix
%Find elements above and below the mean
RPM_L = RPM_c(RPM_c < mean(RPM_c)) ;
RPM_H = RPM_c(RPM_c > mean(RPM_c)) ;
%Perform any desired operation on these elements
%(it could be anything, for this example I choose to smooth & multiply the two groups respectively)
RPM_Ls = smooth(RPM_L, 'rlowess') ;
RPM_Hs = RPM_H * 1.16 ;
Now I need to place RPM_Ls and RPM_Hs back in their original order, as found in vector RPM_c. How could I do this? I am trying to avoid sorting the data. I know this would be easier, however it is of importance in this case that they remain with their current order.
Thanks for your help in advance,
KMT.

Best Answer

Keep track of the original indexing. E.g.,
>> RPM_c = randperm(15) % sample data
RPM_c =
10 9 15 6 13 2 1 11 14 3 8 7 4 5 12
>> x = RPM_c < mean(RPM_c) % logical indexes of the Lower stuff
x =
0 0 0 1 0 1 1 0 0 1 0 1 1 1 0
>> RPM_L = RPM_c(x) % get the Lower stuff
RPM_L =
6 2 1 3 7 4 5
>> RPM_H = RPM_c(~x) % get the Upper stuff
RPM_H =
10 9 15 13 11 14 8 12
>> RPM_L = RPM_L * 2 % do something to the Lower stuff
RPM_L =
12 4 2 6 14 8 10
>> RPM_H = RPM_H / 2 % do something to the Higher stuff
RPM_H =
5.0000 4.5000 7.5000 6.5000 5.5000 7.0000 4.0000 6.0000
>> RPM_c(x) = RPM_L % put the Lower stuff back into original spots
RPM_c =
10 9 15 12 13 4 2 11 14 6 8 14 8 10 12
>> RPM_c(~x) = RPM_H % put the Higher stuff back into original spots
RPM_c =
Columns 1 through 11
5.0000 4.5000 7.5000 12.0000 6.5000 4.0000 2.0000 5.5000 7.0000 6.0000 4.0000
Columns 12 through 15
14.0000 8.0000 10.0000 6.0000