Mathematica - Constructing Matrices

In summary, the conversation discusses constructing a matrix with certain properties, where for n = 8, the matrix would be [16 -16 16 -16 16 -16 16 -16] and for any n, it would be [2n -2n 2n -2n 2n -2n 2n -2n]. The conversation also includes a solution using anonymous function notation and suggests looking up the tutorial on Pure Functions. Finally, alternative solutions using Table and Transpose are also given.
  • #1
sugaku
17
0
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
 
Physics news on Phys.org
  • #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]
 
  • #3
Simon_Tyler said:
Something like
With[{n = 8}, Array[2 n (-1)^(# - 1) &, {n}]]

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

Thank you so much for your help. I do appreciate it. I am not familiar yet with the usage of (-1)^(# - 1)
 
  • #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}]
 
  • #5
If you want a n x 1 matrix, you can also simply do

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

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

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

1. What is Mathematica and how does it relate to constructing matrices?

Mathematica is a powerful software program used for various scientific and mathematical computations. It has built-in functions and algorithms that make it efficient for creating and manipulating matrices. This makes it a popular tool for constructing matrices in various fields such as physics, engineering, and economics.

2. How do I create a matrix in Mathematica?

To create a matrix in Mathematica, you can use the built-in function Array. This function takes in the size of the matrix and the elements you want to include. For example, Array[f, {3,3}] will create a 3x3 matrix with the function f as the elements. You can also use Table or ConstantArray to create matrices with specific patterns or values.

3. Can I perform operations on matrices in Mathematica?

Yes, Mathematica has a wide range of built-in functions for performing operations on matrices. These include matrix multiplication, addition, subtraction, and inversion. You can also use functions like Transpose and Det to manipulate matrices.

4. Can I import and export matrices in Mathematica?

Yes, you can import and export matrices in Mathematica using various file formats such as CSV, Excel, and MATLAB. You can also save matrices as a Mathematica file for future use. Additionally, you can use the MatrixForm function to display matrices in a more visually appealing manner.

5. Are there any resources available for learning more about constructing matrices in Mathematica?

Yes, there are many resources available for learning about constructing matrices in Mathematica. The official Mathematica website has a documentation section with tutorials and examples on matrix operations and constructions. There are also online courses and forums where you can learn from experienced users and ask for help with specific problems.

Similar threads

Replies
3
Views
1K
  • High Energy, Nuclear, Particle Physics
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
406
  • Calculus
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • Calculus and Beyond Homework Help
Replies
11
Views
1K
  • Precalculus Mathematics Homework Help
Replies
12
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Electrical Engineering
Replies
20
Views
2K
Back
Top