Sum over an array with symsum
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Walter Roberson
am 3 Apr. 2019
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.
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!