Reputation: 1
How to create an empty array in matlab that accepts elements from a matrix when you do not know the no.of elements it is going to contain ?
Upvotes: 0
Views: 2752
Reputation: 1097
You can use the empty matrix/vector notation, []
, and Matlab will set up a placeholder for it.
x = []
Now if you want to append a scalar, say num
, to it, you cannot index it because it is empty.
However, you can either:
Use array concatenation to concatenate itself with another scalar:
x = [x num]
Use the end+1
notation, to address the first available location:
x(end+1) = num
Both of the above two notations also work when you want to append a row or a column vector to an existing row vector or column vectors. But when you are concatenating vectors/matrices, remember to be consistent with the dimensions.
Upvotes: 0
Reputation: 495
Use the []
operator. Example:
x = [];
If you wanna be specific in the type of the empty matrix, use the empty
property. Examples:
emptyDoubleMatrix = double.empty; % Same as emptyDoubleMatrix = [];
emptySingleMatrix = single.empty;
emptyUnsignedInt8Matrix = uint8.empty;
This works for empty matrices of classes as well. Example:
emptyFunctionHandleMatrix = function_handle.empty;
Upvotes: 1