Using MATLAB to create a random N,1 array of zeros and ones.
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
dpb
am 15 Sep. 2014
X(randperm(3652,91))=1; % fill a random permutation of 91 locations out of 3652
5 Kommentare
Star Strider
am 15 Sep. 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 Kommentare
Star Strider
am 16 Sep. 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
am 16 Sep. 2014
Bearbeitet: Star Strider
am 16 Sep. 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.
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!