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

74 ビュー (過去 30 日間)
John Morrell
John Morrell 2020 年 3 月 6 日
コメント済み: John Morrell 2020 年 3 月 9 日
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.

採用された回答

John D'Errico
John D'Errico 2020 年 3 月 6 日
編集済み: John D'Errico 2020 年 3 月 6 日
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 件のコメント
J. Alex Lee
J. Alex Lee 2020 年 3 月 6 日
Agree, this would be best for OP since sym is apparently one of the allowed classes for zeros() and ones().
John Morrell
John Morrell 2020 年 3 月 9 日
Thank you, this is exactly what I was looking for! Very nice well explained answer as well.

サインインしてコメントする。

その他の回答 (1 件)

J. Alex Lee
J. Alex Lee 2020 年 3 月 6 日
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

カテゴリ

Help Center および File ExchangeNumber Theory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by