필터 지우기
필터 지우기

How do I create a function that takes a vector of arbitrary length as an input argument and randomly permutes it as an output argument using the identity matrix, without using the randperm command?

조회 수: 4 (최근 30일)
function [ x ] = myPerm(A)
A = ()
myLength = length(A)
I = eye(length(A))
end
this is what I have, I need help creating the arbitrary length and permuting the matrix
  댓글 수: 4
Walter Roberson
Walter Roberson 2018년 12월 11일
you have not put in any version information and full code generation is currently supported for randperm .
If permutation of aa vector is needed then why bother with an identity matrix ?
Sorry but this is sounding like a homework assignment not a real task.

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

답변 (2개)

Stephen23
Stephen23 2018년 12월 11일
편집: Stephen23 2018년 12월 11일
For an array of any size:
>> M = reshape(1:12,3,4)
M =
1 4 7 10
2 5 8 11
3 6 9 12
>> [~,X] = sort(rand(1,numel(M)));
>> M(:) = M(X)
M =
8 9 2 7
5 4 11 1
3 6 12 10

Bruno Luong
Bruno Luong 2018년 12월 11일
Fisher–Yates shuffle
for i = length(A):-1:2
j = ceil(i*rand);
A([i,j]) = A([j i]);
end

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by