# How to custom sort a vector with respect to another vector of unequal length

4 Ansichten (letzte 30 Tage)
Muhammad Usman Saleem am 21 Nov. 2017
I have a vector Lon which I have created after spline interpolation of lon vector
Lon = [...
88.5000
88.0000
87.5000
87.0000
86.5000
86.0000
85.5000
85.0000
84.5000
84.0000
83.5000
83.0000
82.5000
82.0000
81.5000
81.0000
80.5000
80.0000
79.5000
79.0000]
I want to arrange Lon vector with respect to vector lon my lon vector is given below
lon =
88.5000
85.0000
81.0000
79.0000
80.0000
The vector Lon is in descending ordering after interpolation. I want such arrangement of elements in Lon vector
Lon=
88.5
88
87.5
87
86.5
86
85.5
85
84.5
84
83.5
83
82.5
82
81.5
81
79.5
79
80
80.5
Please check the arrangement of 3,4,5 element of lon. I want Lon should be in the same manner of lon .
Please guide me. I am stuck a lot in this sorting!
##### 7 Kommentare5 ältere Kommentare anzeigen5 ältere Kommentare ausblenden
Jan am 27 Nov. 2017
Dear Muhammad: Unfortunately I still do not understand, what the problem is. Which step does not work as you expect it? Where does the unwanted sorting happen? How are Lon and lon in your example related to each other?
Muhammad Usman Saleem am 29 Nov. 2017
Dear @Jan, here is step by step explanation of my code and problem
These lines reading lat and lon vectors from my excel file
lat=file(:,3); % reading lat from file
lon=file(:,2); %reading lon from excel file
Here I am making the newlon vector which will be uniformly spaced start from min value of lon and end with max value of lon along with interval of 0.1
s=min(lon);
e=max(lon);
newlon=s:0.1:e;
generating lat variables from newlon vector with using spline interpolation method as below
newlat=spline(lon,lat,newlon);
Now newlat will be a row vector I want to convert this to column vector by these lines
newlat1=newlat.';
newlon1=newlon.';
lat1=lat.';
lon1=lon.';
After interpolation I see the newlat1 and newlon1 were need to flip up.
Lat=flipud(newlat1);
Lon=flipud(newlon1);
Problem
Now I am comparing the lon1 and newlon1 vector. I saw in lon1 vector, its first element is 85.5, second is 80, third is 81, fourth is 79, fifth is 80.
lon1 =
88.5000
85.0000
81.0000
79.0000
80.0000
I want to sort newlon1 vector such that its element in order all number above 88.5 then from 88.5 to 85 then after 85 to 81 then 81 to 79 and then 79 to 80. Then newlon1 vector will be in the same increasing or decreasing order like lon1 vector.
Here i am getting problem sir? Please assist me

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

John D'Errico am 22 Nov. 2017
Bearbeitet: John D'Errico am 22 Nov. 2017
You appear to be looking to bin your data. Use histcounts. (REALLY!) Read the help.
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Muhammad Usman Saleem am 24 Nov. 2017
Dear I have edited my question. How you are guiding me about hiscounts? Dear this will just count bins in histogram. I want to arrange Lon vector with respect to lon vector. Will you share with me an example you are suggesting me?

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by