MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Using implicit information about row indices

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68314] Re: [mg68254] Using implicit information about row indices
  • From: "Carl K. Woll" <carlw at wolfram.com>
  • Date: Tue, 1 Aug 2006 06:59:40 -0400 (EDT)
  • References: <200607310745.DAA26729@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Diamond, Mark wrote:
> I am wanting to fill a square matrix M (say, r by r)  using information from
> a list, L.
>  The list L is also of length r, but is not a square matrix. It contains
> lists of ordered pairs {col, num}
> It typically looks something like this; I have arranged it into rows so that
> it is more obvious
> {
>   {{1, a}, {2, b}},
> 
>   {{1, c}, {2, d}, {3, e}, {4, f}},
> 
>  {{2, g}, {3, h}, {4, i}},
> 
>  {{3, k}, {4, l}, {5,m}},
> 
>  {{4, n}, {5, p}}
> 
> }
> 
> Now what I am trying to do is, for example, set M[[1,1]]=a, M[[1,2]]=b,
> M[[2,1]]=c, M[[2.2]]=d, M[[2,3]]=e and so forth, and leave (or set) all the
> other entries in M to zero. The row information for M is implicit in the
> structure of the list L; additionally, the column indices as they appear
> within sublist of L are guraranteed to be sequential. It seems with all this
> info, there should be an easy way to fill M correctly, but I have struggled
> without success to do anything other than an iterative process with Do[]. I
> would appreciate any guidance you might have.
> 
> Thank you.
> 
> Mark Diamond
> 
> 
> 

To extract the position information one can use MapIndexed:

L = {{{1, a}, {2, b}}, {{1, c}, {2, d}, {3, e}, {4, f}},
{{2, g}, {3, h}, {4, i}}, {{3, k}, {4, l}, {5, m}}, {{4, n}, {5, p}}};

Here I will use an inert function in MapIndexed so you get an idea of 
what is returned:

MapIndexed[f,L,{2}]

{{f({1,a},{1,1}),f({2,b},{1,2})},{
     f({1,c},{2,1}),f({2,d},{2,2}),f({3,e},{2,3}),f({4,f},{2,4})},{f({2,
       g},{3,1}),f({3,h},{3,2}),f({4,i},{3,3})},{f({3,k},{4,1}),
     f({4,l},{4,2}),f({5,m},{4,3})},{f({4,n},{5,1}),f({5,p},{5,2})}}

The first argument of f is the element of L being mapped over, and the 
second argument is the position of that element. In particular, the 
second argument's first element is the row number. So, we can convert L 
to a list of pos->val rules as follows:

rules = MapIndexed[{#2[[1]], #1[[1]]} -> #1[[2]] & , L, {2}] // Flatten

{{1, 1} -> a, {1, 2} -> b, {2, 1} -> c, {2, 2} -> d, {2, 3} -> e,
{2, 4} -> f, {3, 2} -> g, {3, 3} -> h, {3, 4} -> i, {4, 3} -> k,
{4, 4} -> l, {4, 5} -> m, {5, 4} -> n, {5, 5} -> p}

A list of pos->val rules is the input expected by SparseArray:

matrix = SparseArray[rules];

Here is the normal display of the sparse array:

matrix//Normal

{{a,b,0,0,0},{c,d,e,f,0},{0,g,h,i,0},{0,0,k,l,m},{0,0,0,n,p}}

Carl Woll
Wolfram Research


  • Prev by Date: Re: x=2;Composition[f,FindMinimum][x+1,{x,a}]
  • Next by Date: Re: Re: Using implicit information about row indices
  • Previous by thread: Re: Using implicit information about row indices
  • Next by thread: Re: Using implicit information about row indices