substitute every n elements of a vector
Show older comments
Hi, I have a vector lets say 24 elements (zeros), and I want to substitute a vector of ones (lets say in this example a vector of two ones) n times (lets say 4 times in this example) starting from an index of the original vector decided by me, examples:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (original vector)
1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 (here substituted 4 times a vector of 2 ones starting form index 1)
0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 (here substituted 4 times a vector of 2 ones starting form index 3)
Is it possible to do simply something like this (possibly without a for)? just having the vector and deciding how many times substitute it, and the index to start? Thank you so much.
Accepted Answer
More Answers (1)
Szilárd Ecsenyi
on 22 Jul 2021
0 votes
more faster:
v = 0:1:23;
v(rem(fix(v/3),2) == 0) = 0;
v(v~=0) = 1;
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!