random sorting issue with randperm function

3 views (last 30 days)
Sean Kang
Sean Kang on 12 May 2018
Commented: Stephen23 on 13 May 2018
I'm sorry I'm a beginner of Matlab. To randomly sort my row vector, I used Randperm function. But, the problem is not to sort randomly from my vector values, it seems to make new values.
Actually the original 3D points(x1,y1,z1) should have been the same as randomly sorted points(x1_rand,y1_rand,z1_rand), but as you see in the picture, it's not. ( Purple : Original, Yellow : randomly sorted vectors )
I know the reason why and want to know solutions.
Here's my code :
...
x1(x1==0) = [];
y1(y1==0) = [];
z1(z1==0) = [];
x1_rand = x1(randperm(length(x1)));
y1_rand = y1(randperm(length(y1)));
z1_rand = z1(randperm(length(z1)));
...

Answers (1)

Stephen23
Stephen23 on 12 May 2018
Edited: Stephen23 on 12 May 2018
I suspect that you actually want to use the same sort indices for all three vectors:
idx = randperm(numel(x1));
x1_rand = x1(idx);
y1_rand = y1(idx);
z1_rand = z1(idx);
"Actually the original 3D points(x1,y1,z1) should have been the same as randomly sorted points(x1_rand,y1_rand,z1_rand)"
I don't see why they should be the same: your code explicitly sorts all vectors into different orders. Consider a simpler example, with just two points and two vectors:
X = [1,100]
Y = [2, 99]
Xr = X(randperm(numel(X)));
Yr = Y(randperm(numel(Y)));
there is no reason why Xr and Yr will have to be sorted into the same order, so you could easily get new points (1,99) and (100,2) in some order. These points do not exist in the input data. If you want only the input points (1,2) and (100,99) in some random order, then you need to use the same indices for all vectors.
  4 Comments
Sean Kang
Sean Kang on 12 May 2018
Edited: Sean Kang on 12 May 2018
Oh you're right, I should have sorted three values together. That was the reason why I got a different result. Thank you for help. What I want to do is to sort (x1,y1,z1) ~ (x5,y5,z5) points randomly and trim it. Because it has different size each vector, so I want to sort randomly and extract around 8000 values from each. (Because (x5,y5,z5) points are almost 28000 points but on the other hand (x1,y1,z1) points are 9000 points. )
Stephen23
Stephen23 on 13 May 2018
@Sean Kang: the code I showed you can be used in a loop or called sequentially, so it would be easy to apply this to multiple sets of vectors. Note that rather than using numbered variable names you would be better off using a cell array, which lets you loop over the contents trivially using indexing.
Xc{1} = [...]
Xy{1} = [...]
Xc{2} = [...]
Yc{2} = [...]
...
Yc{N} = [...]
or in a loop:
Xc = cell(1,N)
Yc = cell(1,N)
for k = 1:N
Xc{k} = ...
Yc{k} = ...
end
and then you can easily process them in a loop:
for k = 1:N
Xv = Xc{k}
Yv = Yc{k}
idx = randperm(numel(Xv),8000)
Xv = Xv(idx)
Yv = Yv(idx)
...
end
This will give you a random sample of 8000 points from each input set of points.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!