0

How to create a pre-defined array elements in from single line of code, i.e I have followings array sequence, how can I generate this via a single line of code, as I want to input that to the function argument. It have always the sequence of 0010 and also, 4 columns and 24 rows.

I tried the following:

[0 0 0 1 :24]

The output I want:

                          [ 0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0,
                          0 0 1  0 ]
0

2 Answers 2

2

Use repmat to replicate the data column-wise to create multiple rows -

repmat([0 0 1 0],24,1)

Look into this for more ways to replicate data.

Sign up to request clarification or add additional context in comments.

Comments

0

You can also use matrix multiplication

ones( 24, 1 ) * [0 0 1 0]

Using tic-toc this method seems to be faster than repmat (on my machine):

tic; 
for ii=1:100000, 
    repmat( [0 0 1 0], 24, 1 );
end;
toc, 
tic, 
for ii=1:100000, 
    ones(24,1)*[0 0 1 0];
end;
toc

And the results are

Elapsed time is 1.090915 seconds. // @Divakar's method using repmat
Elapsed time is 0.163920 seconds. // using matrix multiplication

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.