Services & Resources / Wolfram Forums
MathGroup Archive
*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: Initialize a matrix in mathematica

  • To: mathgroup at
  • Subject: [mg66768] Re: Initialize a matrix in mathematica
  • From: Bill Rowe <readnewsciv at>
  • Date: Mon, 29 May 2006 06:06:38 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

On 5/28/06 at 6:08 AM, ashesh.cb at (ashesh) wrote:

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

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

I am not entirely clear as to what you are trying to create. You say matrix which usually means a 2 dimensional array. Yet the code you provide clearly has only one dimension

Your first attempt doesn't work as you would like since you define a to be an empty list. Hence the error message. You could do what you want using a For loop with Append, i.e.,



But while this works, it isn't an efficient way to create this list. Better would be:



and even simpler is


To reply via email subtract one hundred and four

  • Prev by Date: Re: Initialize a matrix in mathematica
  • Next by Date: RE: Initialize a matrix in mathematica
  • Previous by thread: Re: Initialize a matrix in mathematica
  • Next by thread: RE: Initialize a matrix in mathematica