How to input signal to skip numbers and generate data?

I have a set of random 0s and 1s matrix A = [0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 1 0 0 0 0]. I want to create a matrix B that has the same size as A that if i input an interval of say 'n', it skips n numbers, and if it lands on 1, it records 1 but if it lands on 0 it records 0. however, if it doesnt land on 1 for a long time and finally lands on a 1, it records a 1 even though it has skipped more than n numbers. How do I create this matrix B?
For example, n = 2, then matrix B = [0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0]

 Akzeptierte Antwort

Mara
Mara am 27 Jan. 2021
A = [0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 1 0 0 0 0];
n = 2;
B = zeros (1,length(A));
count = 0;
for i = 1:length(A)
count = count + 1;
if A(i) == 1 && count > n
B(i) = 1;
count = 0;
end
end
Does this help you?

5 Kommentare

Thank you so much Mera! You have saved me so much time...
Do you know how I can set it in a way where I can choose where to start the system?
For example, i want to start from the 7th one in matrix A. and n = 2
A = [0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 1 0 0 0 0].
So, B = [0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0]
Mara
Mara am 27 Jan. 2021
Bearbeitet: Mara am 27 Jan. 2021
Hey Leon, glad I could help. If you know the position you want to start with, I think the easiest way might be to include another if statement. If i is taller than or equal to the start position 7, it starts skipping and before that it will just copy the elements of A one by one.
A = [0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 1 0 0 0 0];
n = 2;
B = zeros (1,length(A));
count = n; % the starting value of count needs to be changed if
% you want to be able to start skipping with a 1 right away
start = 7;
for i = 1:length(A)
if i >= start
count = count + 1;
if A(i) == 1 && count > n
B(i) = 1;
count = 0;
end
else
B(i) = A(i)
end
end
or this way, make B a copy of A right at the beginning and then start looping from the starting position.
A = [0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 1 0 0 0 0];
n = 2;
B = A;
count = n; % if you want to be able to start skipping with a 1 right away
start = 7;
for i = start:length(A)
count = count + 1;
if A(i) == 1 && count > n
B(i) = 1;
count = 0;
else
B(i) = 0;
end
end
Thank you so much Mera! It works as usual :)
Hey Mera, how do I force the system matrix A to start from the first “1” it sees? When I try to put n=8, it does not start from the first “1”
Mara
Mara am 29 Jan. 2021
there are many different ways to find the first nonzero position, one is the function find().
Citing the documentation:
k = find(X,n) returns the first n indices corresponding to the nonzero elements in X.
type "doc find" in the command window for more info.
Best,
Mara

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Tags

Gefragt:

am 27 Jan. 2021

Kommentiert:

am 29 Jan. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by