0

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 ?

2
  • This search should have helped you if you had done it. Commented Sep 10, 2014 at 23:33
  • 1
    I agree @ParagS.Chandakkar, however, I am surprised that Mathworks doesn't talk about various ways to add stuff to / expand an empty matrix. They do touch upon array concatenation but end+1 technique is not to be found. Commented Sep 11, 2014 at 0:37

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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:

  1. Use array concatenation to concatenate itself with another scalar:

    x = [x num]
    
  2. 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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.