Reputation: 11
Suppose
A = [1 2 3 5 7 9
6 5 0 3 2 3]
I want to randomize the position of the matrix column position; to give B
like so:
B = [3 9 1 7 2 5
0 3 6 2 5 3]
How can I do this Matlab?
Upvotes: 1
Views: 355
Reputation: 125854
You'll want to use the randperm
function to generate a random permutation of column indices (from 1 to the number of columns in A
), which you can then index A
with:
B = A(:, randperm(size(A, 2)));
Upvotes: 0
Reputation: 30047
Now that code is formatted properly, it's clear the OP wants column preservation, although randperm
is still the easiest option as given by HPM's answer.
idx = randperm(size(A,2)); % Create list of integers 1:n, in random order,
% where n = num of columns
B = A(:, idx); % Shuffles columns about, on all rows, to indixes in idx
Upvotes: 0
Reputation: 78316
Try
B = A(randperm(length(A)))
Consult the documentation for explanations.
Upvotes: 1