How to change alternate four consecutive elements into zeros in an array of ones?

1 view (last 30 days)
I created array of ones [1 1 1 1 1 1 1 1 ...] (size 64)
Now i to convert the alternate four consecutive elements into zeros.
eg: [1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1....]
How do i do this?

Accepted Answer

Image Analyst
Image Analyst on 15 Nov 2021
Assuming it's not your homework (in which case you can't turn it in or risk trouble), here are a few ways:
% Method 1
v = ones(1, 64);
v(5:8:end) = 0;
v(6:8:end) = 0;
v(7:8:end) = 0;
v(8:8:end) = 0
% Method 2
v = ones(1, 64);
v2 = reshape(v, [], 8);
v2(:, 5:8) = 0;
v = reshape(v2', 1, [])
% Method 3
v = ones(1, 64);
v2 = reshape(v, [], 8);
v2(5:8, :) = 0;
v = reshape(v2, 1, [])
  3 Comments
Image Analyst
Image Analyst on 16 Nov 2021
@Samson David Puthenpeedika I hope you at least gave it a try since it's only 3 lines of code. Here is how I did it
% Method 4
v = ones(1, 64);
for k = 5 : 8 : length(v)
v(k:k+3) = 0;
end
v
v = 1×64
1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0
If my answer solved it (or solved it 4 times) then could you please click the "Accept this Answer" link? Thanks in advance.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!