I'm trying to simulate rolling 5 dice for a Yahtzee game I'm writing in MATLAB, but I'm running into the issue that my code doesn't seem to generate any yahtzees after running 1000+ iterations. Am I using a function that will guarantee for a Yahtzee(5 of the same number) to be rolled?
while rounds<=13
fprintf('Rolling the dice...\n');
roll=randi(6,1,5);
roll=sort(roll);
fprintf('You rolled:');
disp(roll);
rollCount=rollCount+1;
for x=rule
if roll==rule{1};
fprintf('Condition Met');
break;
end
end
rounds=rounds+1;
end
This basically just iterates through 13 rounds of rolling and checks the roll against "rule{1}" which is an array that contains [1 1 1 1 1]. There doesn't seem to be a problem getting 3, sometimes 4 1s, but I can't get 5. Any suggestions?
As Ryan Cavanaugh pointed out, there weren't enough samples being done to catch the wanted result. I upped the simulation to run 10000 times and it works as intended.
Related
I'm trying to simulate an optical network algorithm in MATLAB for a homework project. Most of it is already done, but I have an issue with the diagrams I'm getting.
In the simulation I'm generating exponential traffic, however, for low lambda values (0.1) I'm getting very high packet drop rates (99%). I wrote a sample here which is very close to the testbench I'm running on my simulator.
% Run the simulation 10 times, with different lambda values
l = [1 2 3 4 5 6 7 8 9 10];
for i=l(1):l(end)
X = rand();
% In the 'real' simulation the following line defines the time
% when the next packet generation event will occur. Suppose that
% i is the current time
t_poiss = i + ceil((-log(X)/(i/10)));
distr(i)=t_poiss;
end
figure, plot(distr)
axis square
grid on;
title('Exponential test:')
The resulting image is
The diagram I'm getting in this sample is IDENTICAL to the diagram I'm getting for the drop rate/λ. So I would like to ask if I'm doing something wrong or if I miss something? Is this the right thing to expect?
So the problem is coming from might be a numerical problem. Since you are generating a random number for X, the number might be incredibly small - say, close to zero. If you have a number close to zero numerically, log(X) is going to be HUGE. So your calculation of t_poiss will be huge. I would suggest doing something like X = rand() + 1 to make sure that X is never close to zero.
I'm pretty new to Simulink and I was wondering if the following thing was possible somehow:
I have a signal of let's say 10000 data points.
On this signal I want to run a certain algorithm, however said algorithm needs exactly 1000 samples to work properly.
Now with normal matlab functions this is no problem. You cut the signal in 10 pieces, perform the algorithm for each one, stitch the processed parts back together and you get your result.
In Simulink however this creates sort of a problem, since (to my understanding right now) Simulinks blocks work sample per sample (one sample in, one sample out). So I don't have the necessary data to perform the algortihm within a block.
Is there any way to increase the number of processed samples per block?
Reshape 10,000 data points with 1000 samples and create Column wise data
lets say
data = [1 2 3 4 5 6], convert
data = [1 4
2 5
3 6]
Now, define sample time for lets say 1 sec, use fromworkspace blocks. In this example 2 (No of column), with each workspace block format a array as [t data(:,1)],[t data(:,2)] where t = [1 2 3], multiples of sampletime.
On Simulink Model, set running time equal to 3 sec as there are 3 data points and store output via to workspace block
I am trying to generate prime numbers recursively using the previous primes. 2 is a prime by default and each time I keep adding 1 to the next number and divide by the previous primes, eg 2+1=3 so i check that 2 does not divide 3 so 3 is prime so i store 2 and 3 so far, next would be 3+1=4, i would use my current prime list 2 and 3, and see that 2 divides 4 so it does not go into the list then i continue with 4+1 and so forth all the way up to n. The key thing is dividing by the previous primes each time and if you reach a prime the list is updated. Please check my program to see what i am doing wrong.
this is my program so far but I am just getting 3 and 962, i want it update the list and refer back to it each time for the loop so i can use mod(2+numba,primlist) each time:
n=960;
primlist=[];
for numba=2:n
if mod(2+1,2)~=0
primlist=2+1;
end
if mod(2+numba,primlist)~=0
primlist=[primlist;2+numba];
end
end
primlist
You are initializing your primlist again and again. Do not do that. I am making as less modifications to your code to make it run correctly. The logic is essentially correct. You just need to initialize primlist outside.
n=960;
primlist=2;
for numba=1:n %Changed 2 to 1
if mod(2+numba,primlist)~=0
primlist=[primlist;2+numba];
end
end
primlist
Im learning(started today) neural networks and could finish a 2x2x1 network(forward data feeding and backward error propagated) that can learn AND operation for one set of inputs. It also dodges any local minimums using randomized parameters. My first source for this is: http://www.codeproject.com/Articles/14342/Designing-And-Implementing-A-Neural-Network-Librar
The problem is: it learns 0 AND 0 using inputs (0,0) but when I give (0,1) it forgets 0 AND 0 then learns 0 AND 1. Is this a general newbie bug?
What I tried:
loop for 10000 times
learn 0 and 0
end loop
loop for 10000 times
learn 0 and 1 (forgets 0 and 0)
end loop
loop for 10000 times
learn 1 and 0 (forgets 0 and 1)
end loop
loop for 10000 times
learn 1 and 1 (forgets 1 and 0)
end loop
only one set is learned
fail
Trial 2:
loop for 10000 times
learn 0 and 0
learn 0 and 1
learn 1 and 0
learn 1 and 1
end loop
gives same result for all input combinations.
fail.
Activation function for each neuron: hyperbolic tangent
2x2 structure: all-pairs
2x1 structure: all-pairs
Randomized learning rate: yes, small enough to keep far from explosive iteration (per iteration)
Randomized bias per neuron: yes, between -0.5 and +0.5 (just at start)
Randomized weighting: yes, between -0.5 and +0.5 (just at start)
Edit: Bias and weight updates are done for all-pairs of hidden and output layers.
Edit: All neurons(hidden+output) use same activation function.
Without specific code it is hard to say for sure, but I think the issue is that you are only giving it one case to learn at a time. You should give it a matrix of your different learning examples, with an expected result vector. Then, when you update your weights and biases, you are finding the values that minimize the error between your network output for all cases, and the expected output for all cases.
For an AND gate, your input would be (in MATLAB code, not sure what language you are using but that syntax is easy to understand):
input = [0, 0;
0, 1;
1, 0;
1, 1];
And your expected output would be:
output = [0;
0;
0;
1];
I think what you are doing now is basically finding the weights and biases that minimize the error between the network output and the expected output for just one input case, and then re-training those weights and biases to minimize the error for the second case, then the third, then the fourth. If you put them in arrays like this it should minimize the overall error for all cases. This is just my best guess though without any code to go on.
I have a function which for 10 cycles finds the difference between individual sensor values and the average sensor value. The test will be done 100 times using this function. So every time cycle>10 I am forcing it to be zero so that in the 11th repetition it will restart counting from zero. Here is the code:
cycle=cycle +1;
if cycle>10
cycle=0;
end
for i=1: TotalnoOfGrids
for j=1: noOfNodes
if abs(char(Allquants{i}(j))-char(mostCommonLetters {i}))>0
if cycle>0
wrong{i}(j)=wrong{i}(j)+1;
else
wrong{i}(j)=0;
end
end
end
end
Now I need to know if the sensor performed 5 consecutive successes in the period of 10 cycles. How can I do that?
I thought of a loop but I read that it takes too much time.
Doing a search on the net I have found this SO question.
The problem is that the above function will be repeated for 100 cycles.I want for every 10 cycles see if there is consecutive successes so it is beeing done dynamically and I am not saving the success or failure status of the sensor for the cycles. So i do not have a vector containing 1 or 0 to use the function used in the above reference or as Jonas suggested
If a loop is the easiest thing, give it a try! Just because you've read it "takes too much time" doesn't mean it really makes a difference for your case! It is true that in Matlab it often makes sense to avoid loops; but in your case, 100*20*9 (if I understand you correctly) loop iterations doesn't seem so bad yet (depending on your speed requirement).
Edit (corrected answer)
I now understand from your comments that the code you show us is surrounded by a while or for loop which is being run ~100 times, and that Allquants and mostCommonLetters probably change inside that loop. In this case my previous answer didn't work for you, since it counted successes on different sensors; this should be better now.
If I read your code correctly, the condition abs(char(Allquants{i}(j))-char(mostCommonLetters {i}))>0 tells you that a result was "wrong"; consequently,
for i=1:TotalnoOfGrids
this_cycle_successes(i,:)=char(Allquants{i})==char(mostCommonLetters{i});
end
consecutive_successes=(consecutive_successes+1).*this_cycle_successes;
would calculate how many successes you had in a row. Note you need to initialize consecutive_successes before starting your cycle loop, e.g.
consecutive_successes = zeros(9,20);
After the 10 cycles, you can check which sensors had 5 successes like this:
has5successes = consecutive_successes>=5;
Note that this is a matrix operation, so now you will get 9*20 values, as you requested in your comment. This solution wouldn't require a loop over j.