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

1 Ansicht (letzte 30 Tage)
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?

Akzeptierte Antwort

Image Analyst
Image Analyst am 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 Kommentare
Image Analyst
Image Analyst am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by