Matlab: How to random shuffle columns of matrix

2019-02-04 05:39发布

问题:

I have a matrix like:

 A=
    4 7 8 9
    3 3 5 7
    6 4 8 6

and wants to random shuffle columns and do it something like:

 A=
    8 4 9 7
    5 3 7 3
    8 6 6 4

does anyone have any idea?

回答1:

You can shuffle columns using indexing:

A(:,[3 1 4 2])

If you want to do it randomly, you can create a random permutation:

A(:,randperm(size(A,2)));