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

1 visualizzazione (ultimi 30 giorni)
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?

Risposta accettata

Image Analyst
Image Analyst il 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 Commenti
Image Analyst
Image Analyst il 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by