Converting types without symbolic toolbox
6 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Star Strider
2014 年 11 月 30 日
編集済み: Star Strider
2014 年 11 月 30 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!