Sum over an array with symsum
조회 수: 1 (최근 30일)
이전 댓글 표시
I would like to sum over an array of length n using symsum. To give you an exmaple of what I mean, consider
a = [1 2 3]
symsum(a(n), n, 1, 3)
I would like to have the output 1+2+3=6 but I receive the following error (sorry for bad editting, I am new):
Error using sym/subsindex (line 836)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic
variables, and function body must be sym expression.
I know there are other methods, such as sum and for-loop but I would like to use symsum for my purposes. Is there any way that this can be done in Matlab?
Thanks
댓글 수: 0
답변 (1개)
Walter Roberson
2019년 4월 3일
No. It is never possible to use a symbolic variable as a subscript in MATLAB.
Remember that the a(n) part will be evaluated in the process of constructing the arguments of the function, before the function can be evaluated. Therefore a(n) would have to be useable by itself, but it is not.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!