random sorting issue with randperm function
古いコメントを表示
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)));
...
回答 (1 件)
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 件のコメント
" The problem is the randomly sorted scatter points are not the same as the original scatter points. (You can see the picture). So, this is my question.."
And that is what I explain in my answer: you write about "randomly sorted scatter points" but you do NOT sort the points at all... you sort each of the X, Y, and Z values separately. Each point constitutes three values, so if you want to sort the points then you need to sort those three values together. That is what I show in my answer.
Question: Do you want the code to rearrange the points into a random order? This is what I assumed (because you did not explicitly state this in your question). If you want the code to do something else then you will have to explain what it should do in more detail.
Stephen23
2018 年 5 月 13 日
@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.
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!