4
$\begingroup$

This works:

SparseArray[{{1, 1} -> 1, {2, 2} -> 2}]\
(* {{1, 0}, {0, 2}} *)

This works:

SparseArray[{{1, 1}, {1, 2}} -> 1]
(* {{1, 1}} *)

But it seems a combination of this kind of syntax for rules does not, or at least not as I expect it to:

SparseArray[{{{1, 1}, {1, 2}} -> 1, {2, 2} -> 2}]
(* {{0, 0}, {0, 2}} *)

I expected to get

(* {{1, 1}, {0, 2}} *)

which I could obtain by splitting up the positions to be assigned to 1 using

SparseArray[{{1, 1} -> 1, {1, 2} -> 1, {2, 2} -> 2}]
(* {{1, 1}, {0, 2}} *)

but this gets unwieldy if I want to accomplish this for many more points.

Is there something I'm misunderstanding about SparseArray? As near as I can tell, there's nothing in the documentation that explains what's going on here.

Edit: Adding some details about what I plan on doing with this. I'm building an 18x18 array where the entry in the $i$th row and $j$th column represents the "effectiveness" $a_{i,j}$ of a factor $i$ on another factor $j$. Effectiveness is defined by $$a_{i,j}=\begin{cases}2&\text{if very effective}\\1&\text{if neutrally effective}\\\frac{1}{2}&\text{if not very effective}\\0&\text{if completely ineffective}\end{cases}$$ Once created, I was planning on generating a sort of "weighted" graph with this array as the adjacency matrix where the value of $a_{i,j}$ determines the style (say, color or thickness or opacity) of the edge connecting node $i$ to node $j$; essentially a visual representation of the effect $i$ has on $j$. This process is outside the immediate scope of the current question and something I'd like to look into on my own until I run into trouble.

For a concrete example, I'll construct the first row of the array using ciao's suggestion with Alternatives. The $i=1$ factor has effectiveness $$a_{1,j}=\begin{cases}2&\text{if }j\in\{2,10,15\}\\1&\text{if }j\in\{1,3,4,11,12,13,16,18\}\\\frac{1}{2}&\text{if }j\in\{5,6,7,8,9,14,17\}\\0&\text{never}\end{cases}$$ Denoting the effectiveness intensities by

very = {2, 10, 15};
neutral = {1, (*...*), 18};
not = {5, (*...*), 17}; 

I can generate the first row with

SparseArray[
    {
     {1, Alternatives@@very} -> 2,
     {1, Alternatives@@neutral} -> 1,
     {1, Alternatives@@not} -> 1/2
    }, {1, 18}
 ]

letting SparseArray handle the case where $i$ doesn't affect $j$ at all. I was getting an error message about the dimension being undetermined, so I tacked on the {1, 18} at the end to make sure I get an appropriately sized array.

$\endgroup$
  • 1
    $\begingroup$ Workaround: SparseArray[{Thread[{{1, 1}, {1, 2}} -> 1], {2, 2} -> 2} // Flatten] $\endgroup$ – J. M.'s technical difficulties Jun 24 '16 at 2:46
  • 1
    $\begingroup$ Alternatives is your friend here... $\endgroup$ – ciao Jun 24 '16 at 2:52
  • 6
    $\begingroup$ @J.M.SparseArray[{{1, 1 | 2} -> 1, {2, 2} -> 2}] $\endgroup$ – ciao Jun 24 '16 at 3:31
  • 2
    $\begingroup$ @user170231: As far as your question, yes to "Is there something I'm misunderstanding ...": in the form in use here, the rules are expected to be a list of position/value pairs or a list of positions with a rule to 1 value or a list of values of the same length as positions. If you want to nest them as in your non-working example, the right of the rule must be a list, e.g. SparseArray[{{{1, 1}, {1, 2}} -> {1, 1}, {2, 2} -> 2}] - personally, I find patterns/alternatives more transparent. It would be nice if you could expand on "...many more points" with a clear example of your problem. $\endgroup$ – ciao Jun 24 '16 at 4:25
  • 1
    $\begingroup$ Note that you don't need to do the individual alternatives, just use e.g., Alternatives@@very, etc... $\endgroup$ – ciao Jun 25 '16 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.