## how to sort values depending on other vector?

### Shubham Mohan Tatpalliwar (view profile)

on 23 Oct 2018
Latest activity Reopened by Walter Roberson

### Walter Roberson (view profile)

on 22 Dec 2018
if true
% code
x=[0,50,150,100,50,150,0,100,150,0,50,150]
y=[1,2,3,3,2,1,1,2,3,3,2,1]
i want to sort y as 0 to 1, 1 to 2 and 2 to 3
for x 0 to 50 and 50 to 100

### Steven Lord (view profile)

on 23 Oct 2018

If I understand what you've described correctly, you want to sort y and break ties among elements in y that have the same value based on the corresponding elements of x. Is that right? If so, you can pack the two vectors into a matrix and use sortrows to sort first by the column corresponding to y then by the column corresponding to x. In my sample code below, the y column is column 2 and the x column is column 1 so I sortrows using [2 1].
x = [0,50,150,100,50,150,0,100,150,0,50,150]
y = [1,2,3,3,2,1,1,2,3,3,2,1]
M = [x.', y.']
M2 = sortrows(M, [2 1])
If you need to know which row in M went to which row in M2 (or equivalently which elements in x and y went where in M2) call sortrows with two outputs.

Shubham Mohan Tatpalliwar

### Shubham Mohan Tatpalliwar (view profile)

on 23 Oct 2018
What is your ultimate goal here?
to sort the values and count it
If so use histcounts.
yeah i have switched to hit counts but facing the same problem that i could not make a loop
if true
% code
for i =1:1:42
idxi= Achsemoment >=((i-1)*100) & Achsemoment<(i*100);
end
how should i code to create 42 vectors?
Steven Lord

### Steven Lord (view profile)

on 23 Oct 2018
how should i code to create 42 vectors?
How are you planning to use that data? I suspect your ultimate goal is not "to sort the values and count it" -- unless this is part of a homework assignment, I expect you want to do something with the sorted data and/or the counts. What is that something?
Shubham Mohan Tatpalliwar

### Shubham Mohan Tatpalliwar (view profile)

on 23 Oct 2018
from this sorted values
further i want to to sort them on next parameter
like sort(idx1(d>=0 & d<2)
and count the total number of the logical 1
and i want to do it for every vector i.e. every sterp (0 to 100 and so on)
which is time
and this time would be used for further calculations
this is the part of my project and the excel file is bigger so i cannot share it directly on the mathworks portal