random sorting issue with randperm function
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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)));
...
0 Kommentare
Antworten (1)
Stephen23
am 12 Mai 2018
Bearbeitet: Stephen23
am 12 Mai 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 Kommentare
Stephen23
am 13 Mai 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.
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements 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!