With randperm it is possible to mix order of the components of a vector v: v(randperm(lenght(v)))
But how to mix rows in a matrix?

 채택된 답변

Jos (10584)
Jos (10584) 2015년 2월 25일

1 개 추천

Very much the same procedure;
M = repmat(1:10,4,1).' % example data
r = randperm(size(M,1)) % permute row numbers
Mout = M(r,:)

댓글 수: 4

Mr M.
Mr M. 2015년 2월 25일
and how to apply the same permutation to a vector?
Huh? You did that in your question. How is this different than that? To review:
v = [1,2,3,4,5,6,7,8]
v = v(randperm(length(v)))
Mr M.
Mr M. 2015년 2월 25일
but I need the same permutation used for the matrix rows!
Assuming that your matrix has the same number of rows as your vector has elements, then you just need to use the same indices for both the matrix and the vector:
r = randperm(size(M,1)) % generate your indices.
Mout = M(r,:) % rearrange the rows of a matrix
Vout = V(r) % rearrange the elements of a vector
Look how they both use the same permutation r. You should read about indexing.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2015년 2월 25일

댓글:

2015년 2월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by