Use MATLAB to divide random intervals

Source: Internet
Author: User
[Problem description]:If you are interested in the division of random intervals, consider the following:
Divide the Len interval into N segments, and the length of each segment is 0 ~ A random value of M. (Len, n, m is the given value, and N * m> Len ).

Or it can be described as follows:
Generate a series to meet the following requirements (Len, n, m is the given value, and N * m> Len ):
1. There are n columns;
2. Each item of the series is 0 ~ A random value of M;
3. The sum of the items in this series is Len; % %
% MATLAB Solution
% I use a MATLAB program, please comment
% The range with the length of Len is randomly divided into N blocks, and the maximum block cannot exceed M (n × m> Len)
% Algorithm Description:
% 1. First, the first n-1 items A1, A2, A3,..., A (n-1) are generated. each item is 0 ~ Random Number of m;
% 2. Confirm the last item below:
% (1) makes last = len-sum (n-1); sum (n-1) indicates the sum of the first n-1 items
% (2) Test the last value;
% (3) if the last value is equal to: Last> 0 and last <m, last is the last item and the program ends;
% (4) if last <0, an item is randomly selected from the first n-1 item, which is assumed to be the K item,
% Retrieve 0 ~ The random number S of the AK, so that last = last + S,
% (5) if last> m, an AK is randomly selected from the first n-1 items, and 0 ~ is retrieved from the last ~ (M-ak) random number S,
% Last = last-S;
% (6) repeats step (2;
Function f = rand_seq (Len, n, m)
F = m * rand (1, N) % generates a random matrix of 1 row and n columns, each of which cannot exceed M
S = len-(sum (f)-f (n); % last remaining s
Count = 0;
Index = 0;
R = 0;
While S-M> 1e-18 | S <1e-18
While S <1e-18
Index = int16 (Rand * (n-1) + 1 );
T = f (INDEX); % obtains the value of item I.
R = rand;
F (INDEX) = T * r; % extract T * (1-r) from item I and add it to S.
S = S + T * (1-r );
Count = count + 1;
End
While S-M> 1e-18
Index = int16 (Rand * (n-1) + 1 );
T = f (INDEX); % obtains the value of item I.
R = rand;
F (INDEX) = T + (m-t) * R; % add (m-t) * R to item I
S = S-(m-t) * R;
Count = count + 1;
End
End
F (n) = s; % the last item is determined
Count % Number of output cycles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.