vectorizing/simplifying randperm code

조회 수: 2 (최근 30일)
MiauMiau
MiauMiau 2015년 5월 4일
Hi
The idea of my code is the following:
If for instance I have the three "sequences":
(1) 12345 (2) 56789 (3) 43789
And an array of length n, my algorithm choses one of the three sequences at random for array(1). For array(2) the algorithm should chose one of the not yet chosen sequences at random, and array(3) takes the left-over sequence. For array(4) (i.e. arrar(number of sequences +1) the entire procedure restarts. Below my code - could it be done any better/faster?
function x = RandomizeTrials(numberofSeq, lengthofArray, SequenceArray)
N = lengthofArray/numberofSeq;
x = [];
for i=1:N
x = [x randperm(numberofSeq)];
end
for i = 1:length(SequenceArray)
for j = 1:length(x)
if x(j) == i
x(j) = SequenceArray(i);
end
end
end
end

답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by