# Dynamic on the fly expression/function

9 ビュー (過去 30 日間)
Sandeep Parameshwara 2020 年 2 月 8 日

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 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
Sandeep Parameshwara 2020 年 2 月 11 日
1. For i=1
T_dot = T(c)-T(c-2)
2. Yes, you are right. Parantheses is missing
3. Yes, you are right. Parantheses is missing, sorry.
Sandeep Parameshwara 2020 年 2 月 11 日

4. I simplifed the indexing whe I posed the question. It is actually like this, for example for i=2,
T_dot(:,:,1) = T(:,:,c)-T(:,:,c-2)...
+ T(:,:,c+(8*9^0))- T(:,:,c+(8*9^0 - 2*9^1))
For i=3,
T_dot(:,:,1) = 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)))
I am considering all rows and columns and would like to vary only the third index

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

### 採用された回答

Stephen23 2020 年 2 月 11 日

Vector Indices: You can do this quite easily by generating vectors of indices and taking advantage of the fact that addition and subtraction are commutative**. For example:
>> i = 4;
>> c = 4321;
>> X = cumsum([0,8*9.^(0:i-2)]); % explanation below
>> Y = 2*9.^(0:i-1); % explanation below
>> T = rand(1,1e4); % random data matrix
>> sum(T(c+X)-T(c+X-Y))
ans = 0.736941330376921
And now compare against your method (you can see the 15th digit is different, see ** below):
>> 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)))
ans = 0.736941330376922
How it works: For each susbsequent i value you add new terms, but do not change the existing terms. Lets rearrange your i=4 example so that the terms are aligned in columns:
T(c) - T(c - 2) + % same as i=1
T(c+(8*9^0)) - T(c+(8*9^0 - 2*9^1)) + % same as i=2
T(c+(8*9^0)+(8*9^1)) - T(c+(((8*9^0)+(8*9^1) - 2*9^2))) + % same as i=3
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))) % new for i=4
%T(c+X) - T(c+X - Y) % equivalent in my code
The Y term is simpler, so lets start with that: it contains the values
Y = [2, 2*9^1, 2*9^2, 2*9^3]
which is just
Y = 2*9.^(0:i-1)
We can see that the X term is used twice and also involves some kind of cummulative sum. It contains the values
X = [0, 8*9^0, 8*9^0+8*9^1, 8*9^0+8*9^1+8*9^2]
and we notice that each term repeats the previous one plus something new. That new part is just
[0, 8*9^0, 8*9^1, 8*9^2]
which is just
[0,8*9.^(0:i-2)]
and then use cumsum to get the final X vector:
X = cumsum([0,8*9.^(0:i-2)])
The rest is just indexing and addition.
** For numeric computation on binary floating point numbers addition/subtraction are not always commutative, as the results shown above demonstrate. But within reasonable precision we can hold it as being true... as you would anyway not be performing such operations on binary floating point numbers and expecting exact results.
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Sandeep Parameshwara 2020 年 2 月 11 日
Thanks a lot for your help!

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

### その他の回答 (1 件)

Githin John 2020 年 2 月 11 日

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.

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

### カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

R2018b

### Community Treasure Hunt

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

Start Hunting!

Translated by