How do I declare a blank column vector of unknown size?
140 views (last 30 days)
Show older comments
I need to declare a column vector whose elements will be determined as the code runs. How do I declare such a vector of size n (entered by user) in MATLAB.
Thanks and Regards
0 Comments
Accepted Answer
More Answers (2)
Walter Roberson
on 16 Nov 2021
MATLAB does not offer any way of creating blank column vectors of unknown size.
Any numeric column must have some numeric value assigned to the entries. If you write out numeric data to an Excel file, then NaN values will show up empty.
MATLAB allow you to create empty columns. However, if you write data further down in the column, the emptiness is filled with numeric 0 for numeric or logical columns, and by the character with binary value 0 for character columns.
A = repmat('', 5, 1)
A(5,1) = 'Z'
double(A(1,1))
A(1,1) == ' '
The display of the expanded array makes it look like blanks were put in, but you can see that they are binary 0 instead.
Generally speaking, if you have not created a numeric column yet, and at some point in the program you find out how large it will be, then you can create it as the needed size using zeros() or ones() or inf() or nan() . For example,
NumRows = 4
B = ones(NumRows, 2, 'uint8')
If you have not created a character column yet, and at some point in the program you find out how large it will be, then repmat() can sometimes be the easiest way to fill it with blanks:
NumRows = 4
NumCols = 7
C = repmat(' ', NumRows, NumCols)
C(1,1) == ' '
and here you can see this gives true blanks.
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!