how to fill the value of "1" where ever you want in an array.
2 views (last 30 days)
Show older comments
Sandeep Kumar
on 21 Feb 2016
Commented: Image Analyst
on 21 Feb 2016
hello guys i want to fill "1"s in an array where ever i want, for example
for m = 1:64
t = ones(1,((64/4^level)))
if level = 1, then the output should be getting sequence of 16 "ones" filled from 1 to 16, remaining should be zeroes.which is happeneing using my code.
if level = 2, then the output should be getting sequence of 4 "ones" repeated 4 times in the positions from 1, 17,33,49.
end
im using ones(1,((64/4^level))) operation,,,, but for level 2, repetition of 1's sequence in the positions 1,17,33,49 is not happening, im a beginner kindly help me.
4 Comments
Image Analyst
on 21 Feb 2016
Is this homework? Can you try to think up the formula for computing "numberOfOnes" from "N" and "level", like I did in my answer below?
Accepted Answer
Image Analyst
on 21 Feb 2016
I just got a copy of the Mind Reading Toolbox, though it's just valid for this weekend. What I got out was:
% Initialize an output array
output = zeros(1, 64);
% Define level: 1 or 2
level = 2;
% Define the number of elements to assign: 16 or 4.
numElementsToAssign = 16 / level^2
% Define the increment between index numbers: 1 or 16.
increment = 15*level - 14
% Generate the indexes
indexes = 1 : increment : length(output)
% Finally, assign the outputs.
output(indexes(1:numElementsToAssign)) = 1
I gives what you asked for and, I think, gives you what you want, at least for levels 1 and 2 like you asked for. No guarantees if you want something weird for levels 3, 4, etc.
0 Comments
More Answers (1)
dpb
on 21 Feb 2016
_"repetition of 1's sequence in the positions 1,17,33,49..."
Use colon indexing--
t(1:16:LENGTH)=1;
where LENGTH is the maximum length of the vector wanted. The '1' and '16' can be computed and any relationship desired, of course.
I suggest reading and working thru the examples in the "Getting Started" documentation section to get a quick tutorial on Matlab syntax and such basic operations as the quickest way to get over the initial startup problem.
See Also
Categories
Find more on Loops and Conditional Statements 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!