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