Converting types without symbolic toolbox
6 views (last 30 days)
Show older comments
I have a matrix which I want to convert to a list so that I can eliminate zeros. I have a code in Maple for this task, but can't convert to MATLAB. With the Symbolic Toolbox, code to convert a matrix called "T" would look like this:
- coerce(T,DOM_LIST)My question is how to manage this without the toolboxThe Maple code in its entirety follows if there are people out there willing to help with coding the next part.
- L:=convert(T,list)
- remove(has,L,0)
0 Comments
Answers (1)
Star Strider
on 30 Nov 2014
Edited: Star Strider
on 30 Nov 2014
I don’t have much recent experience with Maple so I looked up its definition of ‘list’. If I understand correctly, a list is a vector (or has some similar properties).
This may do what you want:
M = randi([0 9], 5, 5); % Matrix Elements [0,9]
Mv0 = M(:); % Create Vector From Matrix Elements
Mv = Mv0;
Mv(Mv == 0) = []; % Mv With Zeros Removed
It’s less efficient that it could be because I want to be sure it does what you want. It first creates matrix ‘M’ with elements that go from 0 to 9. Then ‘Mv0’ converts it to a vector that includes all the elements including the zeros, and ‘Mv’ duplicates ‘Mv0’. The logical indexing step in the 4th line sets all the zero elements in ‘Mv’ to the empty array [], eliminating them. You can mouse over the various variables to view their contents and be sure the code does what you want. (You may have to run it a few times, since the random number generator may not always create ‘0’ elements.)
A short, efficient version would be:
M(M(:) == 0) = [];
Note that this replaces ‘M’ with a vector containing only its non-zero elements.
0 Comments
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!