Saving integers to vector
显示 更早的评论
Hello
I'm new to Matlab so my question might be simple.
I'm currently coding an "exam-project" in a Matlab course. The goal is to simulate a casino with a roulette table etc. The program in short is:
- You enter the Casino with "x" amount of money
- after each game you have "x" amount of money
- after "n" games you leave the Casino
The program now have to plot a graph showing the players money after each game. That means the money "x" in the y-axis and the number of games "n" in the x-axis. To do this my code in short form is:
% program starts. Asks how much money the user will enter the casino with Bank = "Start amount"
% The program starts a while loop. This while loop will end when the user exit the Casino
% After each game in the casino
Bank = Bank+wonAmount (wonAmount can both be positive and negative)
no_Games = no_Games+1 BankVec(no_Games) = Bank
%while loops ends when the user exit the Casino
plot(BankVec)
I know it would be more effective to pre locate the "BankVec", but since I don't know the number of games the user will play, I don't know how big this pre located vector should be. Is there a more efficient method to store the amount of money after each game?
Best regards
回答(2 个)
Andreas Goser
2012-11-28
0 个投票
I'd say this pretty much depends on how your professor will grade it. Executing speed? Documentation clarity? Size of code?
In order to make a code guess of the size of the preallocated variable, you might want to apply something different than coding skills but simple human sense by thinking of how long a single gamble might take (a minute?) and how long a gambler might be active (4 h?) and then come up with the size.
Jan
2012-11-28
Allocating 1000 elements is as expensive as letting an array grow from 1 to 45 elements - in theory. The current Matlab release promise to handle this more efficiently. Finally the unused elements can be cropped again.
Another common method is to let the array grow in chunks:
tic;
t = [];
for ii = 1:1e5
t(ii) = ii;
end
toc;
tic;
t = [];
t_n = 0; % Max length
t_i = 0; % Current index
t_step = 1000; % Or any "sufficiently large" number
for ii = 1:1e5
t_i = t_i + 1;
if t_i > t_n
t_n = t_n + t_step;
t(t_n) = 0; % Implicit allocation
end
t(t_i) = ii;
end
t = t(1:t_i);
toc;
This reduces the problem of a missing pre-allocation by a factor of t_step. But it is substantially less readable and prone to errors. Therefore for a homework it might be better to use the naive array growing above and add a comment: "Pre-allocation would be smarter, but the programming and debug time would exceed the saved run-time."
- st rule: Never omit the pre-allocation!
- nd rule: ...except if the pre-allocation would mean a "pre-mature optimization" and increase the complexity of the program such that the programming and debugging times get ridiculous compare to the complexity of the problem and the runtime.
类别
在 帮助中心 和 File Exchange 中查找有关 Card games 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!