Selecting non-zero elements from a symbolic vector
Mostra commenti meno recenti
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 Commento
Walter Roberson
il 23 Giu 2015
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?
Risposta accettata
Più risposte (2)
Mischa Kim
il 23 Giu 2015
0 voti
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 Commenti
Farid Salazar Wong
il 23 Giu 2015
Mischa Kim
il 23 Giu 2015
Then you need to post the entire code. Use the paper clip symbol to attach.
Azzi Abdelmalek
il 23 Giu 2015
Modificato: Azzi Abdelmalek
il 23 Giu 2015
syms a b c
d = [a*b, 0 , a^2*b*c, 1, 0]
c=nonzeros(d).'
2 Commenti
Farid Salazar Wong
il 23 Giu 2015
Star Strider
il 23 Giu 2015
The nonzeros function for symbolic arrays was introduced in R2014b.
Categorie
Scopri di più su Linear Algebra in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!