how to fill the value of "1" where ever you want in an array.
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2016-2-21
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?
采纳的回答
Image Analyst
2016-2-21
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 个评论
更多回答(1 个)
dpb
2016-2-21
_"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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!