How to initialize a new matrix of the same type as an existing variable?

74 views (last 30 days)
I'm writing a function that I want to support either numerical or symbolic inputs. It takes in a matrix, initializes a new matrix, then defines it in a for loop. If the input is a syms variable, I'd like to be able to initialize a syms matrix and populate it. If the input is a double, I want to initialize a matrix of doubles. Is there any function that can do this? I can think of some work-arounds that would suffice for this particular example (mostly just not initializing a new matrix before populating in a loop, or setting the new matrix equal to the incoming one before populating) but I'd like to find a neater way to do it.

Accepted Answer

John D'Errico
John D'Errico on 6 Mar 2020
Edited: John D'Errico on 6 Mar 2020
Simplest is to use zeroes!!!!!! Ones will also work. For example, suppose that X is an array of some generic numeric class. It might be uint64, it might be sym, it might be a double. As an example, I've created X as a sym. Now, I want my code to create a new array, of some size, here 1x5.
syms X
A = zeros(1,5,class(X))
A =
[ 0, 0, 0, 0, 0]
whos A
Name Size Bytes Class Attributes
A 1x5 8 sym
As you can see, zeros creates a new array of the desired size and shape, of the designated class. It is filled with zeros. Ones also works, of course. eye even works.
X = uint64(2);
A = eye(5,class(X))
A =
5×5 uint64 matrix
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Are there other ways to do this? Well, yes. If X is a scalar, then as long as the array B does not already exist in the workspace, then you can use the dynamic typing ability of MATLAB to solve the problem.
X = single(3)
X =
single
3
clear B
B(5) = X
B =
1×5 single row vector
0 0 0 0 3
As you can see, B now exists, and is a vector of length 5, entirely zero, except for the last element which is the same as X. Or, I could have used repmat.
C = repmat(X,1,3)
C =
1×3 single row vector
3 3 3
Or, even just a simple indexing, to replicate X.
D = X(ones(1,4))
D =
1×4 single row vector
3 3 3 3
We can overwrite the elements of these arrays as we go, of course. Anything inserted into that array will take on the current class for that variable.
D(2) = 1.25
D =
1×4 single row vector
3 1.25 3 3
So D is still a single precision vector.
  2 Comments
J. Alex Lee
J. Alex Lee on 6 Mar 2020
Agree, this would be best for OP since sym is apparently one of the allowed classes for zeros() and ones().
John Morrell
John Morrell on 9 Mar 2020
Thank you, this is exactly what I was looking for! Very nice well explained answer as well.

Sign in to comment.

More Answers (1)

J. Alex Lee
J. Alex Lee on 6 Mar 2020
I am not sure if this works for symbolic type, but you can initialize an array of arbitrary type by specifying the "last" element first, so if you do
clear;
A(10) = 1
A will end up being a 1x10 array of type double, or of type anything that you put in place of 1.
So in a loop, if you know the extents of your desired final array in each dimension, you can step backwards to 1
for j = 10:-1:1
for i = 20:-1:1
A(j,i) = ...
end
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!