필터 지우기
필터 지우기

How to call element of matrix of symbolic variables

조회 수: 103 (최근 30일)
Hazim Nasir
Hazim Nasir 2018년 7월 16일
편집: Peyman Amiri 2024년 5월 15일
I have two symbolic variables
syms th1(t) Lc1
and generated a matrix in terms of th(t) and Lc1
tc1(t) =
[ cos(th1(t)), -sin(th1(t)), 0, Lc1*cos(th1(t))]
[ sin(th1(t)), cos(th1(t)), 0, Lc1*sin(th1(t))]
[ 0, 0, 1, 0]
[ 0, 0, 0, 1]
now when I try to call the first three elements of the last column
pc1 = tc1(1:3,4);
I got this error:
Error using symfun/subsref (line 169)
Symbolic function expected 1 inputs and received 2.
Error in Fk (line 10)
pc1 = tc1(1:3,4);
thanks

채택된 답변

Walter Roberson
Walter Roberson 2018년 7월 17일
It is not possible to index the entries of a symbolic function that computes a matrix.
The closest you can get is to call the function passing in variables for the parameters, get an array output, index into the array, and use the resulting expression or turn it into a function if needed.
  댓글 수: 10
Walter Roberson
Walter Roberson 2022년 10월 20일
Using () indexing to access the components of a symbolic function that defines an array, is something that is not going to be permitted. The () indexing method for symbolic function is already overloaded to evaluate the function, and there is no way to say "when I write these () brackets I mean indexing instead of evaluation".
There is already formula to retrieve the array expression in a way that can be later indexed.
Walter Roberson
Walter Roberson 2022년 10월 20일
@ermiyas lakew I think you might be looking for argnames

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

추가 답변 (3개)

Nathan Hardenberg
Nathan Hardenberg 2022년 5월 19일
Since the code answer is a bit hidden in the comments I'm posting the solution as full code here:
syms th1(t) Lc1
tc1(t) = [
cos(th1(t)), -sin(th1(t)), 0, Lc1*cos(th1(t));
sin(th1(t)), cos(th1(t)), 0, Lc1*sin(th1(t));
0, 0, 1, 0;
0, 0, 0, 1;]
pc1 = tc1(t); % call the function with t first
pc1 = pc1(1:3,4) % now you can index
  댓글 수: 2
Osvaldo
Osvaldo 2022년 8월 30일
Very usefull! Tks
Stu
Stu 2022년 10월 19일
Wow, thanks for that. It's beautiful.
Now I can pass a vector (which itself contains symbolic variables) into another symbolic function which requires that vector input.

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


João Vitor Fraga
João Vitor Fraga 2023년 2월 1일
편집: João Vitor Fraga 2023년 2월 1일
Here's a simple solution that doesn't turn your symfun into a sym: simply left-multiply and right-multiply your symfun by appropriate matrices whose entries are either zero or one, depending on which elements you wish to extract. Your symfun will remain a symfun. In your case:
syms th1(t) Lc1
tc1(t) = [ cos(th1(t)), -sin(th1(t)), 0, Lc1*cos(th1(t))
sin(th1(t)), cos(th1(t)), 0, Lc1*sin(th1(t))
0, 0, 1, 0
0, 0, 0, 1];
Lmatrix = [1 0 0 0
0 1 0 0
0 0 1 0];
Rmatrix = [0
0
0
1];
pc1 = Lmatrix*tc1*Rmatrix
pc1(t) = 
class(pc1)
ans = 'symfun'
This can easily be condensed into a simple function you could call whenever needed.

Peyman Amiri
Peyman Amiri 2024년 5월 15일
편집: Peyman Amiri 2024년 5월 15일
Even we can make the brilliant soultion of @Nathan Hardenberg better by the following improvement (this solution does not change anything about the initial tc1 matrix):
syms th1(t) Lc1
tc1(t) = [ cos(th1(t)), -sin(th1(t)), 0, Lc1*cos(th1(t));
sin(th1(t)), cos(th1(t)), 0, Lc1*sin(th1(t));
0, 0, 1, 0;
0, 0, 0, 1];
tc1=tc1(t);
pc1 = tc1(1:3,4)
pc1 = 

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by