Re: Using implicit information about row indices
- To: mathgroup at smc.vnet.net
- Subject: [mg68328] Re: [mg68254] Using implicit information about row indices
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Tue, 1 Aug 2006 06:59:59 -0400 (EDT)
- Reply-to: hanlonr at cox.net
- Sender: owner-wri-mathgroup at wolfram.com
sam[L_List] := Module[{sL=Split[L, #1[[1]]<#2[[1]]&]},
SparseArray[Flatten[MapIndexed[Prepend[#1,#2[[1]]]&, sL,{2}],
1]/.{row_, col_, val_}:>({row,col}->val)]];
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}};
sam[L]//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}}
Bob Hanlon
---- "Diamond 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
>