6.24 Doing some matrix operations

Rememebr: Position and Cases return result that can be used by Extract directly. But can’t be used by Part directly.

6.24.1 How to extract first column in matrix

a = Table[RandomInteger[100], {4}, {4}]
 

Out[69]= {{55, 63, 78, 45}, 
          {13, 45, 67, 1}, 
          {94, 32, 48, 90}, 
          {31, 75, 43, 60}}
 

a[[All,1]]
 

Out[70]= {55, 
          13, 
          94, 
          31}
 

6.24.2 How to extract first 3 rows in the first column?

a[[1 ;; 3,1]]
 

Out[71]= {55, 13, 94}
 

6.24.3 How to find some matrix rows based on some condition on value in say the first column??

Find rows which has elements in first column less than 3 in the following

a = {{1, 2, 3}, {4, 5, 8}, {7, 8, 9}}
 

Reference: how-to-extract-rows-from-matrix-based-on-value-in-first-entry

The solution using pattern below (by WRech) is interesting since the same pattern can be used by Cases and Position.

solution by me

pos = Position[a[[All,1]], _?(#1 <= 4 & )] 
Out[73]= {{1}, {2}} 
 
Extract[a, pos] 
Out[74]= {{1, 2, 3}, {4, 5, 8}}
 

by Simon

pos = Position[a, _List?(First[#1] <= 4 & ), {1}] 
Out[75]= {{1}, {2}} 
 
Extract[a, pos] 
Out[76]= {{1, 2, 3}, {4, 5, 8}}
 

By Asim

Pick[a, a[[All,1]], _?(#1 <= 4 & )] 
Out[77]= {{1, 2, 3}, {4, 5, 8}}
 

By WReach

Cases[a,{n_,__}/;n<=4,{}] 
Out[78]= {{1,2,3},{4,5,8}}
 

By WReach

pos=Position[a,{n_,__}/;n<=4,{}] 
Extract[a,pos] 
 
Out[79]= {{1},{2}} 
Out[80]= {{1,2,3},{4,5,8}}
 

6.24.4 How to generate a diagonal matrix?

Random values on the diagonal

DiagonalMatrix[Table[Random[], {3}]]
 

Ones on the diagonal

DiagonalMatrix[Table[1, {3}]]
 

6.24.5 How to generate upper diagonal matrix?

one way, using SparseArray

Normal[SparseArray[{{i_, i_} :> 2*i, {i_, j_} :> i + j /; i < j}, {5, 5}]] 
Out[81]= {{2, 3, 4, 5, 6}, 
          {0, 4, 5, 6, 7}, 
          {0, 0, 6, 7, 8}, 
          {0, 0, 0, 8, 9}, 
          {0, 0, 0, 0, 10}}
 

Or using Table. But notice that in SparseArray, the ’zeros’ are already the default case, so using SparseArray is simpler.

Table[If[i == j, 2*i, If[i < j, i + j, 0]], {i, 5}, {j, 5}] 
Out[82]= ... same as above
 

6.24.6 How to find the trace of a matrix?

see ?Tr[a]

6.24.7 How to find product of elements on the Trace?

a = {{1, 2, 3}, 
     {4, 5, 8}, 
     {7, 8, 9}} 
Tr[a, Times] 
 
Out[84]= 45
 

6.24.8 How to check if a Matrix is diagonal matrix?

by Jon MacLoone

DiagonalQ[m_List] /; ArrayDepth[m] === 2 && Equal @@ Dimensions[m] := 
   And @@ Flatten[MapIndexed[#1 === 0 || Equal @@ #2 & , m, {2}]]; 
 
DiagonalQ[m_] := Return[False]; 
a = {{1, 2}, {2, 4}} 
b = {{1, 0}, {0, 2}} 
 
DiagonalQ[a] 
Out[89]= False 
 
DiagonalQ[b] 
Out[90]= True
 

By Paul Abbott

Clear[DiagonalQ]; 
DiagonalQ[(m_)?MatrixQ] /; SameQ @@ Dimensions[m] := m === DiagonalMatrix[Tr[m, List]] 
DiagonalQ[a] 
 
Out[93]= False 
 
DiagonalQ[b] 
Out[94]= True
 

6.24.9 How to find locations of all zeros (or any other value) in a matrix?

Find location of zeros in this matrix

a = {{1, 2, 3}, 
     {4, 0, 8}, 
     {7, 8, 0}}
 

one way

Position[a, 0] 
Out[96]= {{2, 2}, {3, 3}}
 

Another way

Position[a, _?(#1 == 0 & )] 
Out[97]= {{2, 2}, {3, 3}}
 

6.24.10 How to find locations of elements subject to some test?

find all elements between 4 and 8

a = {{1, 2, 3}, 
     {4, 0, 8}, 
     {7, 8, 0}} 
 
Position[a, _?(#1 >= 4 && #1 <= 8 & )] 
Out[99]= {{2, 1}, {2, 3}, {3, 1}, {3, 2}} 
 
Extract[a, %] 
Out[100]= {4, 8, 7, 8}
 

6.24.11 How to insert an element in specific position?

Using Part to inser 99 in position (1,1)

a = {{1, 2, 3}, 
     {4, 0, 8}, 
     {7, 8, 0}} 
a[[1,1]] = 99; 
a 
 
Out[103]= {{99, 2, 3}, 
           {4, 0, 8}, 
           {7, 8, 0}}
 

6.24.12 How to insert a row into a matrix?

a = {{1, 2, 3}, 
     {4, 0, 8}, 
     {7, 8, 0}}
 

To insert this row in the second row in matrix above

row = {97, 98, 99}; 
newa = Insert[a, row, {2}] 
 
Out[106]= {{1, 2, 3}, 
           {97, 98, 99}, 
           {4, 0, 8}, 
           {7, 8, 0}}
 

or just use ’2’, it will also work

newa = Insert[a, row, 2] 
 
Out[107]= {{1, 2, 3}, 
           {97, 98, 99}, 
           {4, 0, 8}, 
           {7, 8, 0}}
 

6.24.13 How to insert a column into a matrix?

a = {{1, 2, 3}, 
     {4, 0, 8}, 
     {7, 8, 0}}
 

To insert this column in the second column position in above matrix

column = {97, 98, 99};
 

one way

newa = Transpose[Insert[Transpose[a], column, 2]] 
Out[110]= {{1, 97, 2, 3}, 
           {4, 98, 0, 8}, 
           {7, 99, 8, 0}}
 

another way

Normal[SparseArray[{{i_, j_} :> column[[i]] /; j == 2, 
       {i_, j_} :> a[[i,j]] /; j == 1, {i_, j_} :> a[[i,j - 1]] /; j > 1}, 
   {3, 4}]] 
 
Out[111]= {{1, 97, 2, 3}, 
           {4, 98, 0, 8}, 
           {7, 99, 8, 0}}
 

Another way by Leonid Shifrin how-to-insert-a-column-into-a-matrix-the-correct-mathematica-way

MapThread[Insert, {a, column, Table[2, {Length[column]}]}] 
 
Out[112]= {{1, 97, 2, 3}, 
           {4, 98, 0, 8}, 
           {7, 99, 8, 0}}
 

Another by Leonid Shifrin

ArrayFlatten[{{a[[All,1 ;; 1]], Transpose[{column}], a[[All,2 ;; All]]}}] 
 
Out[113]= {{1, 97, 2, 3}, 
           {4, 98, 0, 8}, 
           {7, 99, 8, 0}}
 

6.24.14 How to build a large matrix from blocks of smaller matrices?

Given

a = {{1, 2, 3}, 
     {4, 0, 8}, 
     {7, 8, 0}}
 

and we want to make matrix { {a,a},{a,a} }

b = ArrayFlatten[ {{a, a}, {a, a}}] 
 
Out[118] {{1, 2, 3, 1, 2, 3}, 
          {4, 0, 8, 4, 0, 8}, 
          {7, 8, 0, 7, 8, 0}, 
          {1, 2, 3, 1, 2, 3}, 
          {4, 0, 8, 4, 0, 8}, 
          {7, 8, 0, 7, 8, 0}}]
 

6.24.15 How to apply a function to each element in a 2D matrix?

Given

a = {{1, 2, 3}, 
     {4, 0, 8}, 
     {7, 8, 0}}
 

and we want to apply the this function to it

f[x_] := x + 2*Sin[x]
 

Then using Map

r = Map[f[#1] & , a, {2}] 
 
Out[123]= {{1 + 2*Sin[1], 2 + 2*Sin[2], 3 + 2*Sin[3]}, 
           {4 + 2*Sin[4], 0, 8 + 2*Sin[8]}, 
           {7 + 2*Sin[7], 8 + 2*Sin[8], 0}}