How can I sum the elements of a symbolic vector?

조회 수: 3 (최근 30일)
Shoaibur Rahman
Shoaibur Rahman 2014년 12월 8일
답변: Azzi Abdelmalek 2014년 12월 8일
Hello,
I have a symbolic vector X = [1, x1, x2, x1^2, x2^2, x1*x2], where x1 and x2 are symbols. I want to sum the vector so that it looks like: SUM = 1 + x1 + x2 + x1^2 + x2^2 + x1*x2, that is, in the same order as in X. When I do SUM = sum(X), it gives: SUM = x1^2 + x1*x2 + x1 + x2^2 + x2 + 1.
It is possible by converting X into char first, and then replace the comma (,) by a plus (+). But I wonder if there is an easy way to do this.
Thanks in advance.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 8일
syms x1 x2
X = [1, x1, x2, x1^2, x2^2, x1*x2],
out=char(X(1))
for k=2:numel(X)
out=[out '+' char(X(k))]
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by