Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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: [mg68331] Re: Using implicit information about row indices
  • From: Paul Abbott <paul at physics.uwa.edu.au>
  • Date: Tue, 1 Aug 2006 07:00:03 -0400 (EDT)
  • Organization: The University of Western Australia
  • References: <eakcj6$qce$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

In article <eakcj6$qce$1 at smc.vnet.net>, "Diamond, Mark" <dot at dot.dot> 
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. 

Then SparseArray is your friend.

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

Here is one solution. For

 mat = {

  {{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}}

 };

use MapIndexed and SparseArray:

 Flatten[MapIndexed[{First[#2],First[#1]} -> Last[#1]&, mat, {2}], 1] //
  SparseArray // Normal

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

Cheers,
Paul

_______________________________________________________________________
Paul Abbott                                      Phone:  61 8 6488 2734
School of Physics, M013                            Fax: +61 8 6488 1014
The University of Western Australia         (CRICOS Provider No 00126G)    
AUSTRALIA                               http://physics.uwa.edu.au/~paul


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