필터 지우기
필터 지우기

how to randomly shuffle the row elements of a predefined matrix??

조회 수: 216 (최근 30일)
Suvra Vijay
Suvra Vijay 2017년 2월 14일
답변: Sandip 2023년 10월 15일
i have a matrix , a= [1 2 4 6; 5 8 6 3;4 7 9 1] i want to randomly shuffle the elements of each row. how to do it?? please help

채택된 답변

KSSV
KSSV 2017년 2월 14일
a= [1 2 4 6; 5 8 6 3;4 7 9 1] ;
[m,n] = size(a) ;
idx = randperm(n) ;
b = a ;
b(1,idx) = a(1,:) % first row arranged randomly
  댓글 수: 8
Bruno Luong
Bruno Luong 2022년 4월 24일
@Hamza Shami it shuffle randomly assignmet of columns of a to b

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Tony Richardson
Tony Richardson 2020년 1월 22일
Perhaps not very efficient, but uses only built-in functions and randomizes all elements of all columns:
a = [1 2 4 6; 5 8 6 3;4 7 9 1]
[m, n] = size(a);
[~, idx] = sort(rand(m,n));
b = a(sub2ind([m, n], idx, ones(m,1)*(1:n)))
  댓글 수: 3
Tony Richardson
Tony Richardson 2021년 1월 11일
You can recover the original a matrix with (here c will equal a):
c = ones(size(b));
c(sub2ind([m, n], idx, ones(m,1)*(1:n))) = b
I expect that you want the reverse indexing to go back from b to a, though. This seems to do that (here d will be equal to a):
[~, rdx] = sort(idx);
d = b(sub2ind([m, n], rdx, ones(m,1)*(1:n)))

댓글을 달려면 로그인하십시오.


Jan
Jan 2017년 2월 14일
If you have a C compliler installed, you can try https://www.mathworks.com/matlabcentral/fileexchange/27076-shuffle:
a = [1, 2, 4, 6; 5, 8, 6, 3; 4, 7, 9, 1];
b = Shuffle(a, 2)
  댓글 수: 2
Suvra Vijay
Suvra Vijay 2017년 2월 14일
thank you sir, but I don't have C compiler installed and this 'Shuffle' is not working
Jan
Jan 2017년 2월 14일
While I'm sure, that this Shuffle is working (I've tried it some minutes ago), it requires to be compiled at first.

댓글을 달려면 로그인하십시오.


Nikolay Petrov
Nikolay Petrov 2022년 2월 22일
편집: Nikolay Petrov 2022년 2월 22일
Not sure why this was more complicated to find that it should have been.
a = [1, 2, 4, 6; 5, 8, 6, 3; 4, 7, 9, 1];
a(:, randperm(size(a, 2)))
ans = 3×4
6 4 1 2 3 6 5 8 1 9 4 7
shuffles the elements of each row in the matrix, while
a = [1, 2, 4, 6; 5, 8, 6, 3; 4, 7, 9, 1];
a(randperm(size(a, 1)), :)
ans = 3×4
5 8 6 3 1 2 4 6 4 7 9 1
shuffle the elements of each column in the matrix.
  댓글 수: 1
Tony Richardson
Tony Richardson 2022년 2월 22일
These do the same rearrangement in all rows or columns. Perhaps that is what the original poster wanted. I am not sure. I interpret it as asking for each column (or row) to be shuffled independently of the others.

댓글을 달려면 로그인하십시오.


Tony Richardson
Tony Richardson 2020년 1월 24일
As a variation of my answer above, I'll note that if you want to generate M permutations of N objects (where the N objects are represented by the integers 1-N) you can use:
[~, x] = sort(rand(N, M));
I can generate 100,000 permutations of 52 objects in 0.3 seconds on my machine.
The probability of drawing 3 aces in a 5 card draw can be estimated (using 100,000 dealt hands):
%%%%%%%%%%%%%%%%%
M = 100000; % Number of trials
N = 52; % Number of cards in deck
[~, x] = sort(rand(N, M)); % columns of x are shuffled decks (100,000 shuffled decks)
y = x(1:5,:); % columns of y are the 5 card hands
% N3a is the number of hands containing three Aces out of M (100,000) deals
% I let Aces be cards 1, 14, 27, and 40 (1-13 is one suit, 14-26 is another, etc)
N3a = sum(sum(or(y == 1, y == 14, y == 27, y == 40)) == 3);
P3a = N3a/M
%%%%%%%%%%%%%%%%%
Successive runs of the script gives values of 0.00181, 0.00185, 0.00189, 0.00171.
The theoretical value is 0.001736
  댓글 수: 3
Tony Richardson
Tony Richardson 2021년 1월 11일
To go from the shuffled decks (x) back to the unshuffled decks (z):
z = sort(x)
This would just give a matrix whose columns go from 1-52 in order.
Again, I expect what you want is really the reverse index matrix (rdx), to get that and then recover the unshuffled decks from the shuffled ones:
[~, rdx] = sort(idx);
o = idx(sub2ind([m, n], rdx, ones(m,1)*(1:n)))
rdx would be the reverse indexing matrix. The matrix o would be the unshuffled deck (the same as the matrix z above).

댓글을 달려면 로그인하십시오.


Sandip
Sandip 2023년 10월 15일
a_random = a(randperm(size(a, 1)), :);

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by