Return Matrix to Original Order

1 view (last 30 days)
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.

Accepted Answer

James Tursa
James Tursa on 2 Aug 2017
Edited: James Tursa on 2 Aug 2017
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

More Answers (1)

John BG
John BG on 2 Aug 2017
Hi Konstantinos
1.
Instead of
RPM_L = RPM_c(RPM_c < mean(RPM_c))
RPM_H = RPM_c(RPM_c > mean(RPM_c))
use command find, obtaining the positions of the found elements.
For instance, for the values below mean
RPM_c=randi([-10 10],m,1)
RPM_c =
7
9
-8
9
3
-8
-5
1
10
10
[Li,Lv]=find(RPM_c<mean(RPM_c))
Li =
3
6
7
8
Lv =
1
1
1
1
Lv not used.
Now in Li you have the positions of the elements with values below mean(RPM_c)
Same for values above mean:
[Ui,Uv]=find(RPM_c<mean(RPM_c))
Ui =
3
6
7
8
Uv =
1
1
1
1
2.
Obtaining the values to process
RPM_L=RPM_c(Li)
RPM_H=RPM_c(Ui)
RPM_L =
-8
-8
-5
1
RPM_H =
-8
-8
-5
1
3.
Processing
RPM_Ls = smooth(RPM_L, 'rlowess')
RPM_Hs = RPM_H * 1.16
RPM_Hs = RPM_H * 1.16
RPM_Ls =
-8.3274
-7.1224
-4.1224
0.6726
RPM_Hs =
-9.2800
-9.2800
-5.8000
1.1600
4.
Compiling results
RPM_result=RPM_c;
if any value is exactly the mean value it remains in same position.
RPM_result(Li)=RPM_Ls
RPM_result(Ui)=RPM_Hs
RPM_result =
7.0000
9.0000
-8.3274
9.0000
3.0000
-7.1224
-4.1224
0.6726
10.0000
10.0000
RPM_result(Ui)=RPM_Hs
RPM_result =
7.0000
9.0000
-9.2800
9.0000
3.0000
-9.2800
-5.8000
1.1600
10.0000
10.0000
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
  1 Comment
Konstantinos Tsitsilonis
Konstantinos Tsitsilonis on 3 Aug 2017
Thank you for your answer! I find James Tursa's answer a bit faster, but yours is as functional.

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!