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

5 visualizzazioni (ultimi 30 giorni)
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

Risposte (2)

dpb
dpb il 15 Set 2014
X(randperm(3652,91))=1; % fill a random permutation of 91 locations out of 3652
  5 Commenti
Nick Kyle
Nick Kyle il 16 Set 2014
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

Accedi per commentare.


Star Strider
Star Strider il 15 Set 2014
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 Commenti
Star Strider
Star Strider il 16 Set 2014
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
Star Strider il 16 Set 2014
Modificato: Star Strider il 16 Set 2014
@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.

Accedi per commentare.

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by