5
$\begingroup$

I am having problems with creating a vector of 1s of length (for example) n=4.

I tried

ones=Tuples[{1},4]

This outputs {{$1,1,1,1$}}

The problem I have here, is that I want to concatenate this $1$'s vector with an identity matrix. See below, I transpose the ones vector to get a column vector

onesT = Transpose[ones]

The above command outputs {{$1$},{$1$},{$1$},{$1$}} (which I think is what I want because it denotes a $4 \times 1$ column vector)

I then join with the $4 \times 4$ identity matrix in order to achieve my desired matrix.

identity = IdentityMatrix[4]

matrix = Join[identity, onesT]

However, this outputs {{$1,0,0,0$},{$0,1,0,0$},{$0,0,1,0$},{$0,0,0,1$},{$1$},{$1$},{$1$},{$1$}}

But I want it to output {{$1,0,0,0$},{$0,1,0,0$},{$0,0,1,0$},{$0,0,0,1$},{$1,1,1,1$}}.

Is there a way of either:

  • Creating a vector of $1$'s of length $n$, other than using Tuples[], so as to avoid the formatting issues
  • Concatenating these two matrices (onesT and identity) so as to avoid formatting issues (I tried to use Flatten[] at various stages in the code, and this did not seem to work)
$\endgroup$
6
  • 9
    $\begingroup$ How about: n = 4; Join[IdentityMatrix[n], {ConstantArray[1, n]}] ? $\endgroup$
    – Syed
    Commented Aug 30, 2023 at 15:56
  • 3
    $\begingroup$ Or perhaps: onesT = Table[1, 4] identity = IdentityMatrix[4] matrix = Join[identity, {onesT}] $\endgroup$
    – bmf
    Commented Aug 30, 2023 at 15:59
  • 7
    $\begingroup$ Or Join[IdentityMatrix[n], ConstantArray[1, {1, n}]]. $\endgroup$ Commented Aug 30, 2023 at 16:02
  • 4
    $\begingroup$ Also, SparseArray[{{i_, i_} -> 1, {n + 1, _} -> 1}, {n + 1, n}] or even SparseArray[{i_, i_} | {n + 1, _} -> 1, {n + 1, n}]. $\endgroup$
    – kirma
    Commented Aug 30, 2023 at 16:05
  • 3
    $\begingroup$ Something that has not been advocated yet: keep your original in the following manner: ones = Tuples[{1}, 4] identity = IdentityMatrix[4] matrix = Join[identity, ones] $\endgroup$
    – bmf
    Commented Aug 30, 2023 at 16:23

3 Answers 3

11
$\begingroup$
ArrayPad[IdentityMatrix[n], {{0, 1}, {0, 0}}, 1]

Or

PadRight[IdentityMatrix[n], {n + 1, n}, 1]

Or

Insert[IdentityMatrix[n], ConstantArray[1, n], -1]
$\endgroup$
5
$\begingroup$

You almost got the result you wanted, but somewhere you took a wrong turn. The question states

The problem I have here, is that I want to concatenate this 1's vector with an identity matrix.

The direct code which seems to give the requested output is

ones = Tuples[{1},4];
identity = IdentityMatrix[4];
matrix = Join[ identity, ones]
(* {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}, {1, 1, 1, 1}} *)

This produces a $5\times4$ matrix. However, the question also states

See below, I transpose the ones vector to get a column vector

This is where the wrong turn takes place. There was no need to use a column vector here. The use of Join with matrices having the same number of columns is essentially maintaining the rows of the matrices involved to form the rows of the "joined" matrix.

If, instead, the requirement was to maintain columns, then the use of MapThread with Join would have to be used.

$\endgroup$
2
  • $\begingroup$ The OP wanted a {5,4} matrix. Your solution produces a {4,5} matrix. $\endgroup$
    – lericr
    Commented Aug 31, 2023 at 14:57
  • $\begingroup$ @lericr Thanks for that comment! I will try to fix my answer. $\endgroup$
    – Somos
    Commented Aug 31, 2023 at 15:24
2
$\begingroup$
MapAt[#+1&,IdentityMatrix[{5,4}],5]

(* {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1},{1,1,1,1}} *) 
$\endgroup$

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