Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica - Constructing Matrices

  1. Aug 24, 2010 #1
    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. jcsd
  3. Aug 24, 2010 #2
    Something like
    With[{n = 8}, Array[2 n (-1)^(# - 1) &, {n}]]

    nb the above uses anonymous function notation: eg
    #^2& = Function[x, x^2]
     
  4. Aug 24, 2010 #3
    Thank you so much for your help. I do appreciate it. I am not familiar yet with the usage of (-1)^(# - 1)
     
  5. Aug 24, 2010 #4
    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}]
     
  6. Aug 24, 2010 #5

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    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)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Mathematica - Constructing Matrices
Loading...