how to fill the value of "1" where ever you want in an array.

2 Ansichten (letzte 30 Tage)
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
Sandeep Kumar
Sandeep Kumar am 21 Feb. 2016
Bearbeitet: Sandeep Kumar am 21 Feb. 2016
sure sir, the original code is
N= 16; level =1;
for m = 1:N
t = ones(1,((N/4^level)))
end
%%for a 64 bit only level 1 and level 2 will be present
it was designed by me, its not generic.
k my main aim is if
N= 16 there will only level 1
N= 64 there will only level 1 and level 2
N= 256 there will be level 1 and level 2 and level 3. . .
if N= 16,(level = 1) it should be having 4 ones in the starting sequence, %%working using my code
if N= 64, for level 1, it should have 16 ones from the starting sequence %%working using my code
if N= 64, for level 2, it should be having 4 ones starting in the positions from 1,17,33,49, its like this 1)1,2)1,3)1,4)1,5)0,6)0......17)1,18)1,19)1,20)1,21)0....33)1,34)1..
if N=256, for level 1, it should have 64 ones from the starting sequence %%working using my code
if N= 256, for level 2, it should be having 16 ones starting in the positions from 1,65,129,193 its like this 1)1,2)1,3)1,4)1......,17)0,18)0,21)0....65)1,66)1,67)1....129)1,130)1 .....193)1,194)1
if N=256, for level 3, it should have 4 ones from the starting sequence 1)1,2)1,3)1,4)1,5)0,6)0......17)1,18)1,19)1,20)1,21)0....33)1,34)1.. ..241)1,242)1,243)1,244)1,245)0 thats it., i hope i gave my whole aim of the project.
Image Analyst
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
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.

Weitere Antworten (1)

dpb
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.
  1 Kommentar
Sandeep Kumar
Sandeep Kumar am 21 Feb. 2016
thanku for the answer sir , i tried it, it is working in a way, what i exactly meant is "repetition of 1's sequence in the positions 1,17,33,49..." its like this 1)1, 2)1, 3)1, 4)1, 5)0, 6)0 ...... 17)1, 18)1, 19)1, 20)1, 21)0.... 33)1,34)1.. upto 64 for N=64,level=2.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by