how to fill the value of "1" where ever you want in an array.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sandeep Kumar
am 21 Feb. 2016
Kommentiert: Image Analyst
am 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 Kommentare
Image Analyst
am 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?
Akzeptierte Antwort
Image Analyst
am 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 Kommentare
Weitere Antworten (1)
dpb
am 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.
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!