Using MATLAB to create a random N,1 array of zeros and ones.

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

Answers (2)

X(randperm(3652,91))=1; % fill a random permutation of 91 locations out of 3652

5 Comments

Great! i have the array now, How do i answer the next part. I get it i just need help with the coding.
For each simulated history, find the largest number of crashes occurring in any 8 day
interval. Note that if x is an array of 0’s and 1’s of length n then
y = zeros(n-7,1);
for i=1:n-7
y(i) = sum(x(i:i+7));
end
returns an array where y(i) is the number of crashes in days i, i + 1, i + 2, ..., i + 7. You
can use max(y) to get the largest count of crashes in an 8 day interval.
I am trying to use if and else statements for this last bit,
Sounds like homework. Is it? If so, we don't just give you the answer and you were supposed to tag it as homework. The code you gave would be done more efficiently like
y=conv(X, [1,1,1,1,1,1,1], 'same'); % Compute moving sum in 7 element window
[maxCrashes, indexOfMax] = max(y);
I have done it like this, my first function was Crash1 which was
function X = Crash1 () u=rand(); if u<=91/3652; X=1; else X=0; end X end and then i used this Crashmax() as follows i think it has done it,
function Ymax = Crashmax ()
N=3652; X=zeros(1,N); for i=1:N X(i)=Crash1;
end
y = zeros(N-7,1); for i=1:N-7 y(i) = sum(X(i:i+7)); Ymax=(max(y)); end end
sorry about the tag it was my first question oops

Sign in to comment.

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 Comments

I don't know that that actually gives you the largest number of crashes. I think it just tells you the number of times a crash occurred within 8 days. What would that return for [0 0 0 1 1 0 1 1 0 1 0 0]?
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.
@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.

Sign in to comment.

Asked:

on 15 Sep 2014

Edited:

on 16 Sep 2014

Community Treasure Hunt

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

Start Hunting!