reordering the data

22 ビュー (過去 30 日間)
kash
kash 2012 年 2 月 7 日
i have values as
D=[1 2 3 4 5 6 7 ]
now i want to reorder the data
D1=[3 4 7 1 2 6 5]this is for example,
i want the order to be changed from D,without using randperm function,please help

採用された回答

Andreas Goser
Andreas Goser 2012 年 2 月 7 日
What about generating two random numbers as indices and swapping the data of the indices? And then do this in a loop.
clc
D=[1 2 3 4 5 6 7 ]
for k=1:10
x1=randi(length(D));
x2=randi(length(D));
dummy=D(x1);
D(x1)=D(x2);
D(x2)=dummy
end

その他の回答 (3 件)

Junaid
Junaid 2012 年 2 月 7 日
Second possible way is
D = [1 2 3 4 5 6 7];
declare any temp variable to have the order of indices. For example.
T = [3 4 7 1 2 6 5];
D1 = D(T);
In given example your indices and values are same, but for different values following code will work.

Jan
Jan 2012 年 2 月 7 日
D.E. Knuth has shown some shuffle methods, e.g.:
D1 = D;
for i = 2:numel(D) % Knuth shuffle in forward direction:
w = ceil(rand * i); % 1 <= w <= i
t = D1(w);
D1(w) = D1(i);
D1(i) = t;
end
This is proven to be unbiased and cheaper than using 2 random numbers for swapping. See also: FEX: RPG-lab.

Sean de Wolski
Sean de Wolski 2012 年 2 月 7 日
Sure:
[~, idx] = sort(rand(size(D)));
D1 = D(idx);

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by