symsum of matrix to extract element in the matrix

2 views (last 30 days)
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.

Answers (1)

Ameer Hamza
Ameer Hamza on 10 Jun 2020
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 Comments
Penglin Cai
Penglin Cai on 10 Jun 2020
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 on 10 Jun 2020
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

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!