Dynamic on the fly expression/function
Afficher commentaires plus anciens
Hello,
I want to create a mathematical expression like this. It has certain pattern as given below and number of terms in the expression depends on my input 'i'
given input i=2, matrix T and constant 'c' then some matrix variable T_dot is given by
T_dot = T(c)-T(c-2)...
+ T(c+(8*9^0))- T(c+(8*9^0 - 2*9^1))
if i=3, then
T_dot = T(c)-T(c-2)...
+ T(c+(8*9^0))- T(c+(8*9^0 - 2*9^1))...
+ T(c+(8*9^0)+(8*9^1))- T(c+(((8*9^0)+(8*9^1) - 2*9^2))
if i=4, then
T_dot = T(c)-T(c-2)...
+ T(c+(8*9^0))- T(c+(8*9^0 - 2*9^1))...
+ T(c+(8*9^0)+(8*9^1))- T(c+(((8*9^0)+(8*9^1) - 2*9^2))...
+ T(c+(8*9^0)+(8*9^1)+(8*9^2))- T(c+(((8*9^0)+(8*9^1)+(8*9^2) - 2*9^3))
and so on.... Clearly you can see a pattern in above terms. Basically I have pattern in indices of matrices, but I also want to have these expressions, because later I use T_dot as starting point for 'for loop'. How can I create this? Should I be creating this as a string? or is there any simple way?
Thank you
3 commentaires
" How can I create this?"
Probably using vectorization and/or loops and/or recursion.
"Should I be creating this as a string?"
NO!
If you are able to generate this as a string, then it is also possible to generate it directly as operations on numeric input arguments. Using eval on a string just adds an extra inefficient layer of complexity.
"... is there any simple way?"
Loops and vectorization are simple... recursion a little less so, but still doable.
Could you please clarify a few things:
1- for completeness please provide the output for i=1.
2- mismatching parentheses around the last term of i=3 please confirm:
T(c+(((8*9^0)+(8*9^1) - 2*9^2)))
% ^ missing ?
3- mismatching parentheses around the last term of i=4 please confirm:
T(c+(((8*9^0)+(8*9^1)+(8*9^2) - 2*9^3)))
% ^ missing?
4- You write that the output is "...some matrix variable T_dot ...", but all of the indexing into T appears to be scalar, which means that T_dot would also be scalar. What do you mean that T_dot is a "matrix variable", when your own examples show that it is scalar?
It is an interesting task, if you have some patience I will look at it now...
Sandeep Parameshwara
le 11 Fév 2020
Sandeep Parameshwara
le 11 Fév 2020
Modifié(e) : Sandeep Parameshwara
le 11 Fév 2020
Réponse acceptée
Plus de réponses (1)
Githin John
le 11 Fév 2020
Modifié(e) : Githin John
le 11 Fév 2020
I am assuming that when you say you want to have the expressions for later use, you mean you want to store the indices as an expression. I would suggest using a cell array containing strings. Something like this.
T_dot_exp = {'T(c)' '-T(c-2)'...
'T(c+(8*9^0))' '-T(c+(8*9^0 - 2*9^1))'...
'T(c+(8*9^0)+(8*9^1))' '-T(c+(((8*9^0)+(8*9^1) - 2*9^2))'...
'T(c+(8*9^0)+(8*9^1)+(8*9^2))' '-T(c+(((8*9^0)+(8*9^1)+(8*9^2) - 2*9^3))'}
or just the indices alone:
T_dot_exp = {'(c)' '(c-2)'...
'(c+(8*9^0))' '(c+(8*9^0 - 2*9^1))'...
'(c+(8*9^0)+(8*9^1))' '(c+(((8*9^0)+(8*9^1) - 2*9^2))'...
'(c+(8*9^0)+(8*9^1)+(8*9^2))' '(c+(((8*9^0)+(8*9^1)+(8*9^2) - 2*9^3))'}
To use the value of the first expressions you may try
eval(T_dot_exp{1})
Edit: Thank you for pointing out the problems with my suggestions. I read up on your write-up and the MATLAB docs about misuse of eval. Enlightened.
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!