Length of Array of Symbolic Variables

조회 수: 22 (최근 30일)
Nathan Batta
Nathan Batta 2020년 1월 24일
댓글: Walter Roberson 2023년 10월 6일
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
Alex Mcaulley
Alex Mcaulley 2020년 1월 24일
편집: Alex Mcaulley 2020년 1월 24일
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é
René 2023년 10월 6일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Dyuman Joshi
Dyuman Joshi 2023년 10월 6일
이동: Walter Roberson 2023년 10월 6일
@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)
lA = 1
sA = size(A)
sA = 1×2
1 1
lB = length(B)
lB = 4
sB = size(B)
sB = 1×2
4 1
whos
Name Size Bytes Class Attributes A 1x1 8 symfun B 4x1 8 sym a 1x1 8 symfun b 1x1 8 sym c 1x1 8 sym cmdout 1x33 66 char d 1x1 8 sym e 1x1 8 sym lA 1x1 8 double lB 1x1 8 double sA 1x2 16 double sB 1x2 16 double t 1x1 8 sym
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 10월 6일
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(t) = 
C(2)
ans = 
C(1)
Error using indexing
Division by zero.
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.

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by