I'm struggling a little to understand your goal, I've coded up what you described (as I read it), but I suspect I'm missing something as your text doesn't quite make sense. Nonetheless, I noticed some potential bugs in your code if it's helpful:
- you're generating two random numbers in your loop, which seems odd,
- your logic statements don't seem to match your code - you wrote AND which would be represented as && but coded OR which is ||.
Feel free to clarify and I'm happy to take another attempt...the more you can simplify your question the easier it will be to help.
Here's my attempt to interpret as code based on your text:
Your text is missing one condition...
- If the last index was A and the random number is <=0.5, the new index should be the A.
- If the random number is >0.5 then the new index shoud be B.
You haven't accounted for the case where the last index was B, which implies a 'neither' state?
You also depend on the 'last' index, but that would mean you need some intial value. I'll make the initial value of A based on rand <= 0.5.
isA = false(1,30);
isB = false(1,30);
isA(1) = rand <= 0.5; % need some initial value as all remaining values depend on previous values
isB(1) = false;
for i = 2:30
r = rand;
if isA(i-1) && r <= 0.5
% If the last index was A *AND* the random number is <=0.5, the new index should be the A.
isA(i) = true;
elseif r>0.5
% If the random number is >0.5 then the new index shoud be B.
isB(i) = true;
end
end
