2
$\begingroup$

How to write a matrix of $n\times 2$ where the first column is made of only 1's?

Suppose I have a vector x={.24,...,10}.

How can I get this

$\left( \begin{matrix} 1 & .24 \\ 1 & 21 \\ 1 & 33 \\ 1 & 11 \\ 1 & 10 \end{matrix}\right)$

How can I do that?

Help me please

I know how to write a 'normal' matrix, it's MatrixForm[{1,2},{1,1}] but how to give the form above?

$\endgroup$
1

2 Answers 2

5
$\begingroup$
col2 = {2.4, 21, 33, 11, 10};
Thread[{1, col2}]

{{1, 2.4}, {1, 21}, {1, 33}, {1, 11}, {1, 10}}

% // MatrixForm //TeXForm

$\left( \begin{array}{cc} 1 & 2.4 \\ 1 & 21 \\ 1 & 33 \\ 1 & 11 \\ 1 & 10 \\ \end{array} \right)$

Also

{1, #} & /@ col2 (* or Map[{1, #} &, col2] *)
col1 = ConstantArray[1, Length@col2];
Transpose[{col1, col2}]

both give

{{1, 2.4`}, {1, 21}, {1, 33}, {1, 11}, {1, 10}}

$\endgroup$
2
  • $\begingroup$ Many thanks kglr, I think the first option its the easier:) $\endgroup$ Oct 31, 2018 at 5:07
  • 1
    $\begingroup$ @user459663, my pleasure. Welcome to mma.se. $\endgroup$
    – kglr
    Oct 31, 2018 at 5:14
1
$\begingroup$

Here is another solution, based on Table. For a toy list,

bill = Range[5]
Flatten[Table[{1, bill[[j]]}, {i, 1, 1}, {j, 1, 5}], 1]
MatrixForm[ted]

The last line is for formating reasons, and Flatten removes a redundant pair of brackets.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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