Re: Initialize a matrix in mathematica

• To: mathgroup at smc.vnet.net
• Subject: [mg66752] Re: [mg66742] Initialize a matrix in mathematica
• From: Bob Hanlon <hanlonr at cox.net>
• Date: Mon, 29 May 2006 06:05:21 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```ndiv=5;
a=Table[0,{ndiv}];
For[i=1,i<=ndiv,i++,a[[i]]=i*i];
a

{1,4,9,16,25}

Here are some other ways:

a=Table[i^2,{i,ndiv}]

{1,4,9,16,25}

a=Range[ndiv]^2

{1,4,9,16,25}

Bob Hanlon

---- ashesh <ashesh.cb at gmail.com> wrote:
>
> Hi All,
>
> Have programmed in another system until now and am trying to shift to
> Mathematica and facing some diffciulties
>
> I would like to create a matrix, say a[ndiv] and would like to
> initilize all the value to i^2 (or any other value). The size of the
> matrix is not fixed.
>
> The code I tried to use is:
>
> ndiv = 5;
> a = {};
> For[i=1, i<=ndiv, i++,
> a[[i]] = i*i;
> ];
> a
>
> This is producing the following error:
> partw: Part 1 of {} does not exist and so on.
>
> I understand that I am not creating a matrix of size 1*ndiv for the
> data to be fed it.
>
> So I tried using Table as follows:
>
> a = Table[0,{1},{ndiv}] followed by the above For loop (when I wanted
> to initialize the matrix with say i*i;
>
> Hope some one can help me in doing the above operation
>
> Regs.
>

```

• Prev by Date: FilledListPlot with StackGraphics
• Next by Date: Re: Defining N for a new entity
• Previous by thread: Re: Initialize a matrix in mathematica
• Next by thread: Re: Initialize a matrix in mathematica