Message Boards Message Boards

4 Replies
2 Total Likes
View groups...
Share this post:

SparseArray and non-numerical elements

Posted 10 years ago

Re 10.0.1 under Windows 7.

The SparseArray documentation says entries need not be numeric. Why then do I get the error message below? Here the main diagonal entries are lists (off-diagonal entries will be the default zero).


In[129]:= SparseArray@{{1, 1} -> {"a", "b"}, {2, 2} -> {"c", "d", "e"}}

During evaluation of In[129]:= SparseArray::posr: The left-hand side of {2,2}->{c,d,e} in {{1,1}->{a,b},{2,2}->{c,d,e}} is not a position or a pattern that will match the position of an element in an array with depth 1. >>
POSTED BY: Bruce Colletti
4 Replies

The SparseArray documentation also says that

The individual elements of a sparse array cannot themselves be lists.

POSTED BY: Ilian Gachevski

This might not help for your application, but you can define the elements as Strings and then later "unpack them" should you need to do so:

array = SparseArray@{{1, 1} -> "{\"a\",\"b\"}", {2, 2} -> "{\"c\",\"d\",\"e\"}"}

(I needed to escape the "-symbols.) The command gives you a sparse array. You can later reconvert them to an expression:

array[[1, 1]] // ToExpression

This might not work for your application. I have one for which it was useful.



POSTED BY: Marco Thiel
Posted 10 years ago

Thanks, Illian. I hadn't seen that second bullet -- I could only see that what I had wanted to do was allowed by the first bullet.

I've sent Tech Support a suggestion to merge the two bullets into one -- that would've avoided the problem for me.


POSTED BY: Bruce Colletti
Posted 10 years ago

Marco, your idea is much better than my workaround! And so I'll use your idea. Thank you. Bruce

POSTED BY: Bruce Colletti
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract