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

1 次查看(过去 30 天)
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?

采纳的回答

Image Analyst
Image Analyst 2021-11-15
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 个评论
Image Analyst
Image Analyst 2021-11-16
@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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by