Octave if when loop problem

1 Ansicht (letzte 30 Tage)
Verity Rothermel
Verity Rothermel am 14 Jan. 2021
Kommentiert: Rik am 14 Jan. 2021
Hi
I'm trying to program an exponential distibution with Octave. I want to save the values for x and s. My two problems are that the loop stops as soon as even one of the s(j) is >30, instead of when all of them are. And that when I write y(j)=rand(1), it gives me a matrix full of the same random number, but I want 30 different random numbers. When I write y=rand(30,1), then I get an error saying the the dimentions are 1x1 and 1x30 and that they don't match.
function[x,w]=wartezeitExp4(t,lambda)
n=10;
t=30;
lambda=4;
y=0;
s=0;
x=0;
while s<t
s=s+1
for j=1:30
x(j)=(log(1-rand(30,1))./(-lambda));
s(j)=s+x;
endfor
disp (t)
end
end
  3 Kommentare
Verity Rothermel
Verity Rothermel am 14 Jan. 2021
The idea is that x_n is an interval between s_n and s_n+1. "s" is an event. The simulation is supposed to stop when s>t. THe vaiables that I want to keep are the last two "s" values, so the ones before and after t, w=s-t and the very last value for x. Getting these values once is no problem, but I want to repeat the "experiment" 1000 times and the plot all the x, s and w values. That's why I'm trying to save them in the matrix j with the if loop, but I don't know how.
Rik
Rik am 14 Jan. 2021
Why don't you write a function that will run your expirement once and return the relevant variables. Then you can easily run that in a loop to do 1000 experiments.
Als a side-note: please make sure your question applies to Matlab as well. This site is maintained by Mathworks and it would be in poor taste to repay that kindness by providing a solution that will only work on Octave. There is no harm in making sure the code works on both platforms (I actively confirm that for my FEX submissions), but my personal line is that it should at least run on Matlab as well. Your interpretation of the terms of service might be different.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by