How do I declare a blank column vector of unknown size?
109 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(2 个)
Walter Roberson
2021-11-16
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!