Mathematica - Constructing Matrices

  • Context: Mathematica 
  • Thread starter Thread starter sugaku
  • Start date Start date
  • Tags Tags
    Mathematica Matrices
Click For Summary

Discussion Overview

The discussion revolves around constructing matrices in Mathematica with specific properties, particularly focusing on generating a 1 by n matrix with alternating values based on the input parameter n. The conversation includes various approaches and functions within Mathematica to achieve this matrix construction.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant describes a desired matrix structure for n = 8 and proposes a general form for n, suggesting a pattern of alternating values.
  • Another participant provides a solution using the Array function with an anonymous function notation to generate the matrix based on the described properties.
  • A similar solution is reiterated, emphasizing the use of anonymous functions and providing a brief explanation of the notation used.
  • A further response offers an equivalent solution by defining a named function and using it within the Array function to create the matrix.
  • Another participant suggests alternative methods using the Block and Table functions to create either a column matrix or a transposed version, noting the importance of applying MatrixForm for correct visualization.

Areas of Agreement / Disagreement

Participants present multiple approaches to constructing the desired matrix, indicating a lack of consensus on a single method, but all contributions aim to address the same problem of matrix construction.

Contextual Notes

Some participants express uncertainty about specific Mathematica functions and notations, indicating a potential gap in familiarity with the language's features, such as anonymous functions and matrix formatting.

Who May Find This Useful

This discussion may be useful for Mathematica users seeking to understand matrix construction techniques, particularly those interested in generating matrices with specific alternating patterns and those looking for different methods to achieve similar results.

sugaku
Messages
16
Reaction score
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
Something like
With[{n = 8}, Array[2 n (-1)^(# - 1) &, {n}]]

nb the above uses anonymous function notation: eg
#^2& = Function[x, x^2]
 
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)
 
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}]
 
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)
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
49
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
13K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K