Search code examples
rvectorconcatenationmatrix-multiplication

When I concatenate in R am I creating a row or a column?


I concatenate the following:

ExampleConCat <- c(1, 1, 1, 0) and I have a 20x4 matrix (MatrixExample as below).

I can do matrix multiplication in Rstudio as below:

matrix.multipl <- MatrixExample %*% ExampleConCat 

I get the below results:

#              [,1]
# cycle_1  0.99019608
# cycle_2  0.96400149
# cycle_3  0.91064055
# cycle_4  0.83460040
# cycle_5  0.74478532
# cycle_6  0.64981877
# cycle_7  0.55637987
# cycle_8  0.46893791
# cycle_9  0.39005264
# cycle_10 0.32083829
# cycle_11 0.26141338
# cycle_12 0.21127026
# cycle_13 0.16955189
# cycle_14 0.13524509
# cycle_15 0.10730721
# cycle_16 0.08474320
# cycle_17 0.06664783
# cycle_18 0.05222437
# cycle_19 0.04078855
# cycle_20 0.03176356

My understanding is that:

To multiply an m×n matrix by an n×p matrix, the ns must be the same, and the result is an m×p matrix. https://www.mathsisfun.com/algebra/matrix-multiplying.html

So, the fact that it calculates at all indicates to me that concatenate above creates a column, i.e.: MatrixExample is a 20X4 matrix, thus ExampleConCat must be a 4X1 vector, in order for these two to multiply by eachother.

Or, are there different rules when one multiplies a vector by a matrix, and could you explain those to me simply?

I noticed that when I tried

matrix.multipl <- ExampleConCat %*% MatrixExample 

I get the following:

Error in ExampleConCat %*% MatrixExample : non-conformable arguments

I would appreciate an explanation which reflects that I am new to R and newer still to matrix multiplication.

# MatrixExample:

#                State A   State B     State C     State D
# cycle_1  0.721453287 0.201845444 0.06689735 0.009803922
# cycle_2  0.520494846 0.262910628 0.18059602 0.035998510
# cycle_3  0.375512717 0.257831905 0.27729592 0.089359455
# cycle_4  0.270914884 0.225616773 0.33806874 0.165399604
# cycle_5  0.195452434 0.185784574 0.36354831 0.255214678
# cycle_6  0.141009801 0.147407084 0.36140189 0.350181229
# cycle_7  0.101731984 0.114117654 0.34053023 0.443620127
# cycle_8  0.073394875 0.086845747 0.30869729 0.531062087
# cycle_9  0.052950973 0.065278842 0.27182282 0.609947364
# cycle_10 0.038201654 0.048620213 0.23401643 0.679161707
# cycle_11 0.027560709 0.035963116 0.19788955 0.738586622
# cycle_12 0.019883764 0.026460490 0.16492601 0.788729740
# cycle_13 0.014345207 0.019389137 0.13581754 0.830448113
# cycle_14 0.010349397 0.014162175 0.11073351 0.864754914
# cycle_15 0.007466606 0.010318351 0.08952225 0.892692795
# cycle_16 0.005386808 0.007502899 0.07185350 0.915256795
# cycle_17 0.003886330 0.005447095 0.05731440 0.933352173
# cycle_18 0.002803806 0.003949642 0.04547092 0.947775632
# cycle_19 0.002022815 0.002860998 0.03590474 0.959211445
# cycle_20 0.001459366 0.002070768 0.02823342 0.968236444

Solution

  • If you check the help section help("%*%"), it briefly describes the rule for matrix multiplcation is used for vectors.

    Multiplies two matrices, if they are conformable. If one argument is a vector, it will be promoted to either a row or column matrix to make the two arguments conformable. If both are vectors of the same length, it will return the inner product (as a matrix).

    Doing MatrixExample %*% ExampleConCat, as you rightly pointed out conforms to those rules, ExampleConCat is treated as a 4 by 1 matrix. But when ExampleConCat %*% MatrixExample is done, the dimensions don't match i.e. ExampleConCat has 4*1 (or 1*4) whereas MatrixExample has 20*4 as dimension.

    The vector will be converted to either row or column matrix, whichever makes the matrix work, as an example please see below:

    exm = c(1,1,1,0)
    
    exm_matrix = matrix(rnorm(16), 
                        ncol=4)
    
    exm_matrix%*%exm
    #>            [,1]
    #> [1,]  2.1098758
    #> [2,] -1.4432619
    #> [3,] -0.2540392
    #> [4,] -0.4211889
    
    exm%*%exm_matrix
    #>          [,1]       [,2]       [,3]      [,4]
    #> [1,] 1.161164 -0.3602107 -0.3883783 -1.580562
    

    Created on 2021-07-02 by the reprex package (v0.3.0)