Matlab单行向量,同时取代值的循环

时间:2016-05-22 22:11:55

标签: matlab for-loop vector probability montecarlo

{{1}}

100人排队,前排的人坐在一个随意的座位上,其他人都按顺序排队。如果一个人找到他们的座位,他们会坐在另一个随机座位上。我试图找到座位100在排队的最后一个人到达时打开的概率。我的问题是如何让我的while循环查找一个打开的座位(等于0)并为其分配一个,有问题的部分是在Else语句之后。

2 个答案:

答案 0 :(得分:0)

每次拨打randi时,您都会获得不同的号码,因此您可以再次将占用的座位分配给1。您应该使用while true,生成一次随机数,然后break如果座位未被占用则退出循环。您可以通过跟踪座位号而完全消除该循环,并且只从空座位中选择:

numtrials = 10000;
empty = 0;
for i = 1:numtrials
    seat = 1:100; %List all seat numbers
    seat = seat(seat~=randi(100)); %Remove a random seat from the list of seats
     for p = 2:99   % second person in line will look for seat 2, only  up to 99 because I want to see if 99 is zero
         if any(seat == p)
             seat = seat(seat~=p);
         else
             s = seat(randi(length(seat))); %Randomly select an empty seat
             seat = seat(seat~=s); %Eliminate this seat
         end
     end
     if any(seat == 100)
         empty = empty + 1;
     end
end
disp(empty)
 X = empty/numtrials;   
disp(['The probability that your seat will be available is ', num2str(X)])

编辑: 第一个建议是:

while true
  s = randi(100);
  if (seat(s) == 0)
    seat(s) = 1;
    break;
  end
end

答案 1 :(得分:0)

1/2

它被称为“奶奶”问题(Gradma是第一个选择随机座位的人)。没有必要编码任何东西,概率是<?php class portfolio_metabox { public function __construct() { if ( is_admin() ) { add_action( 'load-post.php', array( $this, 'init_metabox' ) ); add_action( 'load-post-new.php', array( $this, 'init_metabox' ) ); } } public function init_metabox() { add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) ); add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 ); } public function add_metabox() { add_meta_box( 'details', __( 'Project Details', 'chic' ), array( $this, 'render_project_metabox' ), 'portfolio', 'advanced', 'default' ); } public function render_project_metabox( $post ) { // Retrieve an existing value from the database. $portfolio_web_design = get_post_meta( $post->ID, 'portfolio_web_design ', true ); $portfolio_web_dev = get_post_meta( $post->ID, 'portfolio_web_dev ', true ); $portfolio_digital_art = get_post_meta( $post->ID, 'portfolio_digital_art ', true ); $portfolio_graphic_design = get_post_meta( $post->ID, 'portfolio_graphic_design ', true ); // Set default values. // Form fields. echo '<table class="form-table">'; echo '<tr>'; echo '<th><label for="portfolio_project" class="portfolio_project_label">' . __( 'Project Type', 'chic' ) . '</label></th>'; echo '<td>'; echo '<label><input type="checkbox" name="portfolio_web_design " class="portfolio_project_field" value="' . $portfolio_web_design . '" ' . checked( $portfolio_web_design , 'checked', false ) . '> ' . __( ' Web Design', 'chic' ) . '</label><br>'; echo '<label><input type="checkbox" name="portfolio_web_dev " class="portfolio_project_field" value="' . $portfolio_web_dev . '" ' . checked( $portfolio_web_dev , 'checked', false ) . '> ' . __( ' Web Development', 'chic' ) . '</label><br>'; echo '<label><input type="checkbox" name="portfolio_digital_art " class="portfolio_project_field" value="' . $portfolio_digital_art . '" ' . checked( $portfolio_digital_art , 'checked', false ) . '> ' . __( ' Digital Art', 'chic' ) . '</label><br>'; echo '<label><input type="checkbox" name="portfolio_graphic_design " class="portfolio_project_field" value="' . $portfolio_graphic_design . '" ' . checked( $portfolio_graphic_design , 'checked', false ) . '> ' . __( ' Graphic Design', 'chic' ) . '</label><br>'; echo '<p class="description">' . __( 'Project Type', 'chic' ) . '</p>'; echo '</td>'; echo '</tr>'; echo '</table>'; } public function save_metabox( $post_id, $post ) { // Sanitize user input. $portfolio_new_web_design = isset( $_POST[ 'portfolio_web_design' ] ) ? 'checked' : ''; $portfolio_new_web_dev = isset( $_POST[ 'portfolio_web_dev' ] ) ? 'checked' : ''; $portfolio_new_digital_art = isset( $_POST[ 'portfolio_digital_art' ] ) ? 'checked' : ''; $portfolio_new_digital_art = isset( $_POST[ 'portfolio_graphic_design' ] ) ? 'checked' : ''; // Update the meta field in the database. update_post_meta( $post_id, 'portfolio_web_design ', $portfolio_new_web_design ); update_post_meta( $post_id, 'portfolio_web_dev ', $portfolio_new_web_dev ); update_post_meta( $post_id, 'portfolio_digital_art ', $portfolio_new_digital_art ); update_post_meta( $post_id, 'portfolio_graphic_design ', $portfolio_new_digital_art ); } } new portfolio_metabox; ?> :)

证明的简明版本:第一个坐在奶奶座位上的人,或者第100个座位决定结果。在所有可能的情况下,所有人(包括奶奶)在奶奶座位上与第100个座位的座位概率完全相同。完全对称。