Clear Filters
Clear Filters

What is the meaning of this simple 1 line matlab code?

2 views (last 30 days)
X = [x, zeros(1,n)];
x is a 1xN matrix.

Answers (1)

Alok Nimrani
Alok Nimrani on 6 Apr 2018
Hi Minsoo Kim,
zeros(1,n) creates a 1xn vector of zeros i.e.
>> zeros(1,5)
ans =
0 0 0 0 0
You can read more about ‘zeros’ at the following link: https://www.mathworks.com/help/matlab/ref/zeros.html
Now, ‘x’ is a 1xn vector and the operation being performed is:
>> X = [x zeros(1,n)];
Here, ‘[ ]’ is the matrix constructor operator and when the elements within the brackets are separated by space, the resulting matrix is a row-wise concatenation of the elements. So, the above operation is basically a row-wise concatenation of ‘x’ and ‘zeros(1,n)’. Check the following code as an example:
>> x = ones(1,5)
x =
1 1 1 1 1
>> X = [x zeros(1,5)]
X =
1 1 1 1 1 0 0 0 0 0
For more information about the matrix constructor operator '[ ]', you can check the following link: https://www.mathworks.com/help/matlab/math/creating-and-concatenating-matrices.html#f1-84906
Hope this clears your query.
Thanks.

Categories

Find more on Creating and Concatenating Matrices 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!