Hi Guys, CODE BELOW
I have a function where I have the variable coun, coun is calculated
I then have a function which runs that function multiple times for a given number.
What I would like to do is run the function twice, so lets say i run it first time and then get coun = -1 i want to then use that value of coun and not start again at zero. Could someone please help me with this, thanks Steven.
CODE BELOW for calculation of count.
function [x,money] = winnings4(x)
i0 = (randi(length(x)));
j0 = (randi(length(x)));
k0 = (randi(length(x)));
while j0==i0
j0 = (randi(length(x)));
end
while k0==j0
while k0==i0
k0 = (randi(length(x)));
end
if k0==j0
k0 = (randi(length(x)));
end
end
PlayersHand = [x(i0),x(j0)]
DealersHand = [x(k0)]
a = x(i0)
b = x(j0)
c = x(k0)
coun = 0;
if a < 7 && a >= 2
coun = coun + 1
end
if a < 10 && a > 6
coun = coun + 0
end
if a == 10
coun = coun - 1
end
if a == 1
coun = coun - 1
end
if b < 7 && b > 2
coun = coun + 1
end
if b < 10 && b > 6
coun = coun + 0
end
if b == 10
coun = coun - 1
end
if b == 1
coun = coun - 1
end
if c < 7 && c > 2
coun = coun + 1
end
if c < 10 && c > 6
coun = coun + 0
end
if c == 10
coun = coun - 1
end
if c == 1
coun = coun - 1
end
x(i0) = 0;
x(j0) = 0;
x(k0) = 0;
x = x(x~=0)
%%----------------------------------------CODE FOR FUNCTION TO RUN ABOVE MULTIPLE TIMES BELOW:----------------------%%
function run4 = m()
v = [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10];
deck = [v,v,v,v,v,v]; % 6 decks
y = zeros(1,100);
for k = 1:2
[deck,y(k)] = winnings4(deck);
if length(deck) < 78
run4 = sum(y);
p = length(deck);
q = k;
return
end
end
run4 = sum(y);
end

 採用された回答

darova
darova 2020 年 3 月 20 日

1 投票

6 件のコメント

Steven Strange
Steven Strange 2020 年 3 月 20 日
hi,
I am quite new to MATLAB, what do you mean by persistent?
thanks, Steven
darova
darova 2020 年 3 月 20 日
I mean that variable you declared as persistent is not removed after function calling
function main
func
func
end
function func
persistent a
if isempty(a)
a = 1;
end
a = a + 1
end
To clear variables (these lines should be outside outside the function (command window or script))
clear functions
Steven Strange
Steven Strange 2020 年 3 月 20 日
hi, this does not carry through my value of coun.
My problem is, if the function is ran multiple times the value of coun starts again at = 0, i dont want this, i want the value of coun to start off at whatever it was in the previous run.
For exampe
if coun = 5
the next run should start with coun = 5 NOT 0.
Hope this helps. Steven.
Ameer Hamza
Ameer Hamza 2020 年 3 月 20 日
You need to declare your variable coun as persistent. Replace the following line in your function
coun = 0;
with
persistent coun
if isempty(coun)
coun = 0;
end
darova
darova 2020 年 3 月 20 日
I made a simple scheme for you. Please see it and try to understand
Steven Strange
Steven Strange 2020 年 3 月 20 日
hi, that works! thank you so much Ameer and Darova

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Introduction to Installation and Licensing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by