Re: mathematica newbie ?
- To: mathgroup at smc.vnet.net
- Subject: [mg92423] Re: mathematica newbie ?
- From: "David Park" <djmpark at comcast.net>
- Date: Tue, 30 Sep 2008 21:50:10 -0400 (EDT)
- References: <gbt2pe$lcc$1@smc.vnet.net>
Jared, Table is the command to construct lists and arrays. A vector is just a list. Switch or Which are the commands for returning different things with different values. It is almost always better to localize variables such as nsupports, ndof and nels within a structure or definition. You can use With or Module to do that. Or you can make them arguments in a routine that generates your vector. You don't have to distinguish between a row vector and a column vector. If you write an expression such as vector.matrix or matrix.vector Mathematica handles it properly where vector is just a list. You can use Column to display a vector in column form. With[ {nsupports = 3, ndof = 10, nels = 2}, Table[ Switch[x, 0, 0, nsupports - 1, ndof - 2, _, 2 nels x], {x, 0, nsupports - 1}]] {0, 4, 8} With[ {nsupports = 3, ndof = 10, nels = 2}, Table[ Which[ x == 0, 0, x == nsupports - 1, ndof - 2, True, 2 nels x], {x, 0, nsupports - 1}]] {0, 4, 8} myvector[nsupports_, ndof_, nels_] := Table[ Switch[x, 0, 0, nsupports - 1, ndof - 2, _, 2 nels x], {x, 0, nsupports - 1}] myvector[3, 10, 2] {0, 4, 8} -- David Park djmpark at comcast.net http://home.comcast.net/~djmpark/ <Jared.webb at yahoo.com> wrote in message news:gbt2pe$lcc$1 at smc.vnet.net... >I am extremely new to mathematica and am having trouble following the > documentation. > > I would like to set up the following loop and have it send the output > to a vector. > > If I have in mathematica: > > nsupports=3 > ndof=10 > nels=2 > int=range[0,nsupports-1] - this is my iterator > > I want to set up a loop: > > for x in int: > if x=0, return 0 > if x=nsupports-1, return ndof-2 > otherwise, return x*nels*2 > > I want the output to go to a column vector. > > Thanks > Jared >