Using MATLAB to create a random N,1 array of zeros and ones.
5 ビュー (過去 30 日間)
古いコメントを表示
The question is that there have been 91 major plane crashes in 10 years, so i am trying to create a matrix of 3652 days,I am using a for loop to fill the matrix such that
N=3652;
X=zeros(1,N)
for i=1:N;
i=rand()
if i<=91/3652;
X(i)=1;
else
X(i)=0;
(Obviously wrong)
and then i need to find the maximum number of plane crashes in 8 days, so i am trying to find the probability of the recent plane disasters.
My code for finding this is
y = zeros(n-7,1);
for i=1:n-7
y(i) = sum(x(i:i+7));
end
Your help is much appreciated note probability of a crash = 91/3652
0 件のコメント
回答 (2 件)
dpb
2014 年 9 月 15 日
X(randperm(3652,91))=1; % fill a random permutation of 91 locations out of 3652
5 件のコメント
Star Strider
2014 年 9 月 15 日
This is how I would do it:
d = zeros(1, 3652); % ‘Days’ Vector
c = randi(3652, 1, 92); % Random Event Locations
p(c) = 1; % Assign Events to Days
ck = find(p == 1); % Check Event Locations
frq = diff(ck); % Find Day Differences
frq8 = find(frq <= 8); % Find Differences < 8 Days
There may be more efficient methods, but this should work.
5 件のコメント
Star Strider
2014 年 9 月 16 日
Continuing from my previous code ...
A version of Image Analyst’s Comment that I was working on simultaneously use the filter function in the form of a moving average filter:
b = ones(1,8); % Filter Denominator
a = sum(b); % Filter Numerator
D8 = filter(b, a, p); % Filter the ‘p’ Vector, Then Sort...
[DF, DFI] = sort(D8, 'descend');
dv = 1:size(d,2); % Serial Day Vector
figure(1)
stem(dv, p) % Plot Aircraft Mishaps & Filter Output
hold on
plot(dv, D8, 'r', 'LineWidth',1.5)
hold off
grid
axis([0 3653 0 1.1])
Multiply the filter output ‘D8’ by 8 to get the number of mishaps in any 8-day period. (The plot does that.) I’ll let you analyse the output of sort. The plot depicts the data and the output of the filter.
Star Strider
2014 年 9 月 16 日
編集済み: Star Strider
2014 年 9 月 16 日
@Sean — It’s a moving average, so it looks at every consecutive 8-day segment, [1:8], [2:9], ... and gives the number in every segment.
I’m simply offering a way to find the frequency of the mishaps over time. I’m more familiar with filters.
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!