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

조회 수: 5 (최근 30일)
Nick Kyle
Nick Kyle 2014년 9월 15일
편집: Star Strider 2014년 9월 16일
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

답변 (2개)

dpb
dpb 2014년 9월 15일
X(randperm(3652,91))=1; % fill a random permutation of 91 locations out of 3652
  댓글 수: 5
Nick Kyle
Nick Kyle 2014년 9월 16일
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

댓글을 달려면 로그인하십시오.


Star Strider
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
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
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 CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by