필터 지우기
필터 지우기

Selecting non-zero elements from a symbolic vector

조회 수: 4 (최근 30일)
Farid Salazar Wong
Farid Salazar Wong 2015년 6월 23일
댓글: Star Strider 2015년 6월 23일
Given a symbolic vector, say [a*b, 0 , a^2*b*c, 1, 0]. How could I obtain a vector from the non-zero entries, I would like for the given example to have [a*b, a^2*b*c, 1] as a result. I tried nonzeros but this does not work for type sym.
  댓글 수: 1
Walter Roberson
Walter Roberson 2015년 6월 23일
Do you want to remove the entries that are identical to 0, or the entries that can be proven to be 0? For example if one of the entries was 1 - cos(theta)^2 - sin(theta)^2 then we could show that this must be 0 (at least for finite non-nan values), so should it be removed?

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2015년 6월 23일
This works for me (in R2015a):
syms a b c
V = [a*b, 0 , a^2*b*c, 1, 0];
Vnz = V(V~=0)
Vnz =
[ a*b, a^2*b*c, 1]
  댓글 수: 2
Farid Salazar Wong
Farid Salazar Wong 2015년 6월 23일
Appreciated, that is all I needed! You are a boss.
Star Strider
Star Strider 2015년 6월 23일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Mischa Kim
Mischa Kim 2015년 6월 23일

Farid, you could use

syms a b c
d = [a*b, 0 , a^2*b*c, 1, 0];
d(d==0) = []
d = [ a*b, a^2*b*c, 1]
  댓글 수: 2
Farid Salazar Wong
Farid Salazar Wong 2015년 6월 23일
I get the following message (which I am not sure what it means): Error using symfun>validateArgNames (line 205) Second input must be a scalar or vector of unique symbolic variables.
Error in symfun (line 45) y.vars = validateArgNames(inputs);
Error in sym/subsasgn (line 762) C = symfun(B,[inds{:}]);
Mischa Kim
Mischa Kim 2015년 6월 23일
Then you need to post the entire code. Use the paper clip symbol to attach.

댓글을 달려면 로그인하십시오.


Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 23일
편집: Azzi Abdelmalek 2015년 6월 23일
syms a b c
d = [a*b, 0 , a^2*b*c, 1, 0]
c=nonzeros(d).'
  댓글 수: 2
Farid Salazar Wong
Farid Salazar Wong 2015년 6월 23일
nonzeros do not work for type sym.
Star Strider
Star Strider 2015년 6월 23일
The nonzeros function for symbolic arrays was introduced in R2014b.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by