Length of Array of Symbolic Variables
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello! I have an array of several symbolic variables that I want to loop through. However, when I use the length function, it says the length of the array is 1. Does anyone know the issue? Thank you!
2 commentaires
Alex Mcaulley
le 24 Jan 2020
Modifié(e) : Alex Mcaulley
le 24 Jan 2020
Can you upload your variable in a mat file? It should work
syms a b c d
A = [a;b;c;d];
length(A)
ans =
4
René
le 6 Oct 2023
Apparently, the length and size functions don't work if the symbolic variables are dependent. For instance,
syms t
syms a(t) b c d e
A = [a;b;c;d];
B = [b;c;d;e];
lA = length(A)
sA = size(A)
lB = length(B)
sB = size(B)
returns
lA =
1
sA =
1 1
lB =
4
sB =
4 1
Réponses (1)
Dyuman Joshi
le 6 Oct 2023
Déplacé(e) : Walter Roberson
le 6 Oct 2023
@René Hochdahl, that is because a(t) is not a symbolic variable, but a symbolic function (see below).
As a(t) is a symbolic function, A is also defined as a symbolic function. symfun objects, like function handles, are 1x1 in size.
syms t
syms a(t) b c d e
A = [a;b;c;d];
B = [b;c;d;e];
lA = length(A)
sA = size(A)
lB = length(B)
sB = size(B)
whos
1 commentaire
Walter Roberson
le 6 Oct 2023
This is correct.
If you have a symbolic function in an expression and you are not invoking the expression with specific parameters, then the datatype of the result of the expression is almost always symfun -- and symfun are scalar .
It is not possible to have a [] array that has a symfun as an indexable component.
syms a(t)
C = [a, 5/(t-1)]
C(2)
C(1)
You can see that C(2) is not indexing C at location 2, and is instead invoking C with parameter 2, and C(1) is not indexing C at location 1, and is instead invoking C with parameter 1. C was created as a single function that returns an array, not as an array of functions or an array in which the first element is a function and the second is a non-function.
Voir également
Catégories
En savoir plus sur Conversion Between Symbolic and Numeric dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!