# Mathematica - Constructing Matrices

1. Aug 24, 2010

### sugaku

Good day to all,

I am stuck with this. I am trying to construct a matrices with this properties....

if n = 8, suppose the matrix with size 1 by 8 become
[16 -16 16 -16 16 -16 16 -16]

if n the matrix become [2n -2n 2n -2n 2n -2n 2n -2n ] with size 1 by n

I do appreciate if someone could give me hint on this... thank you in advance

2. Aug 24, 2010

### Simon_Tyler

Something like
With[{n = 8}, Array[2 n (-1)^(# - 1) &, {n}]]

nb the above uses anonymous function notation: eg
#^2& = Function[x, x^2]

3. Aug 24, 2010

### sugaku

Thank you so much for your help. I do appreciate it. I am not familiar yet with the usage of (-1)^(# - 1)

4. Aug 24, 2010

### Simon_Tyler

Not a prob.

I thought you might not be familiar with anom functions, which is why I gave its full mma name: Function. You can look it up in the documentation center - and read the tutorial on Pure Functions (tutorial/PureFunctions).

An equivalent solution where we give the function a name is
f[n_][x_] := 2 n (-1)^(x-1)
Array[f[8], {8}]

5. Aug 24, 2010

### CompuChip

If you want a n x 1 matrix, you can also simply do

Code (Text):

Block[{n = 8},
Table[(-1)^j * 2n, {j, 1, n}
]

or

Code (Text):

Block[{n = 8},
Transpose[Table[(-1)^j * 2n, {j, 1, n}]]
]

(apply MatrixForm to see which way it comes out correctly)