SparseArray and non-numerical elements

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).

Thanks.

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. >>

The SparseArray documentation also says that

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

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.

Cheers,

Marco

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.

Bruce

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