3
$\begingroup$

I need to input the following list of values for x and y , each as a column matrix (7-row, 1-column). Then, combine the two into a 2 x 7 column matrix. I'm new to Mathematica. Please help. How do I do this? Thank you.

x = 1.13 | 2.56 | 3.42 | 4.75 | 5.39 | 6.84 | 7.25
y = 3.83 | 4.15 | 3.79 | 1.78 | 0.23 | −0.76 |  −1.25
$\endgroup$
1
  • 1
    $\begingroup$ You could just input them as a list (e.g. x = {1.13, ..., 7.25}) and Transpose x and y afterwards. $\endgroup$ May 17 at 22:53

1 Answer 1

8
$\begingroup$

Mathematica uses the list for almost everything. You can think of a list as a row for now for purposes of making matrices.

So when you write {{1,2,3,4},{5,6,7,2}} then this is a matrix of two rows.

To convert this to matrix of 2 columns, you transpose it. So by transposing at the end, you are telling Mathematica, these rows are really columns.

Transpose[{{1, 2, 3, 4}, {5, 6, 7, 2}}]

Mathematica graphics

So always start with the idea that lists are rows. To do what you want for your data:

x = {1.13, 2.56, 3.42, 4.75, 5.39, 6.84, 7.25}
y = {3.83, 4.15, 3.79, 1.78, 0.23, -0.76, -1.25}
mat = {x, y}

Mathematica graphics

The above is matrix with 2 rows. But you want these to be columns, not rows. Then do an extra step

mat = Transpose[mat]

Mathematica graphics

You'll see this alot in Mathematica when working with matrices. You construct the matrix as rows, since that is the natural way for Mathematica, then add a transpose at the end. This is a little different from Matlab.

$\endgroup$
2
  • $\begingroup$ Thank you Nasser. I get it now from your explanation. $\endgroup$ May 18 at 13:52
  • $\begingroup$ (+1) and a minor comment for completeness: Thread@mat is equivalent to the Transpose@mat $\endgroup$
    – bmf
    May 18 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.