symsum of matrix to extract element in the matrix

1 次查看(过去 30 天)
i want to sum this:
The code i used:
syms j
S=[0,1,0,0,0;1/2,0,1/2,0,0;0,1/2,0,1/2,0;0,0,1/2,0,1/2;0,0,0,1,0];
C1=symsum(S(1,j),j,1,5);
The code reterned me error. I want to do the polynomial summation as shwon above. l also would like to extract the element in matrix S for different j. Could you tell me what is the correct code i need to use? Thank you.

回答(1 个)

Ameer Hamza
Ameer Hamza 2020-6-10
Symbolic variables cannot be used as vector index. Also, your data is numeric, and it seems that you want to sum rows. Try this
S = [0,1,0,0,0;1/2,0,1/2,0,0;0,1/2,0,1/2,0;0,0,1/2,0,1/2;0,0,0,1,0];
C1 = sum(S, 2);
If you want to output in symbolic form
S = [0,1,0,0,0;1/2,0,1/2,0,0;0,1/2,0,1/2,0;0,0,1/2,0,1/2;0,0,0,1,0];
C1 = sum(sym(S), 2);
  2 个评论
Penglin Cai
Penglin Cai 2020-6-10
Thank you for the answer. But I do not want to sum the rows, l want to extract certain element in the matrix for different j. For example, i want to S11 for j=1, S12 for j=2.
Ameer Hamza
Ameer Hamza 2020-6-10
Try this
S = [0,1,0,0,0;1/2,0,1/2,0,0;0,1/2,0,1/2,0;0,0,1/2,0,1/2;0,0,0,1,0];
f = @(j) S(1, j);
Result
>> f(1) % j=1 => S11
ans =
0
>> f(2) % j=2 => S12
ans =
1

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by