How can I randomly shuffle columns in my matrix

36 views (last 30 days)
For example, if I have
1 5 9 13 17
2 6 10 14 18
3 7 11 15 19
4 8 12 16 20
I want to have a function that can shuffle the columns around while keeping the values in the column the same. For example:
1 9 13 5 17
2 10 14 6 18
3 11 15 7 19
4 12 16 8 20
The columns have been randomly rearranged.

Answers (1)

Walter Roberson
Walter Roberson on 20 Oct 2020
randperm() the number of columns and use the result as the column indices
A(:, randperm(size(A, 2)))

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!