Return Matrix to Original Order
이전 댓글 표시
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.
채택된 답변
추가 답변 (1개)
John BG
2017년 8월 2일
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
카테고리
도움말 센터 및 File Exchange에서 Managing Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!