フィルターのクリア

How to output as a vector

1 回表示 (過去 30 日間)
Lewis Watson
Lewis Watson 2012 年 10 月 30 日
ive got a coin toss simulation game where score is kept.
function = coin_toss(s(0),N)
% coin toss simulation
% a coin is tossed n times, at each point a score is given +1 for heads
% and -1 for tails.
N=input(10)
for k=1:N
rand_number=rand;
if random_number<0.5
random_number(k,1)=-1;
else
random_number(k,1)=1;
for i=1:N,
s(0)=-1
s(N)= s(N-1)+random_number
end
end
end
I basicly want to have an output of the scores after each flip divided by the flip number in a vector e.g
[(-1/1),(s(1)/2),(s(2),3)........ (s(N)/(N+1)] [-1,

採用された回答

José-Luis
José-Luis 2012 年 10 月 30 日
編集済み: José-Luis 2012 年 10 月 30 日
What's the meaning of N = input(10)? I am guessing that what you wanted to do is to pass N to a function. But, if so, why ask for its value again inside the function? And if you know the first value is always -1 you don't really need to pass it either.
If you want to return a vector
function your_result = coin_toss(N);
%Preallocating for your_result:
your_result = zeros(N,1);
%Perform your calculations
your_result(1) = -1;
for ii = 2:N
%do your stuff
your_result(ii) = %You might know how to do this since it has already been answered
end
%You might also know how to do the rest. Hint: cumsum()

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMuPAD についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by