jeudi 5 mars 2015

Generating an equal number of random integers in response to a previous input in Matlab

I'm trying to create a random number matrix in matlab. However, I'm struggling a little with the logic of some of it. What I want is this:


I need it to loop through a predefined random matrix (2 rows, n columns) 50% of which are 1's and 50% are 0's (I can already do this part). Everytime it encounters a 1 it should then enter another loop which puts a 1,2,3 or 4 in the corresponding position in the second row. However (and this is the part I'm struggling with) I need it to have an equal number of 1's, 2's, 3's and 4's in the second row. So for example:


The matrix n = [0, 1, 0, 1, 1, 1, 0, 0; 0,0,0,0,0,0,0,0] should run through the script and produce something like: n = [0, 1, 0, 1, 1, 1, 0, 0; 1, 3, 2, 4, 4, 2, 1, 3]


This is what I have so far:



function pureToneTimer
ptpschedule = [0, 1, 0, 1, 1, 1, 0, 0; 0, 0, 0, 0, 0 , 0, 0, 0]
a = 0;
b = 0;
c = 0;
d = 0;
x = length(ptpschedule)/4
for n = 1:length(ptpschedule)
if ptpschedule(1,n) == 1
while a < x && b < x && c < x && d < x
i = randi(4)
ptpschedule(2,n) = i;
switch i
case 1
a = a + 1;
case 2
b = b + 1;
case 3
c = c + 1;
case 4
d = d + 1;
end
end
end
end
assignin('caller', 'ptpschedule', ptpschedule)
end


Sorry if this turns out to be a really trivial question. I'm just struggling to wrap my head around it!


Thanks,


Martin





Aucun commentaire:

Enregistrer un commentaire