R - matrix multiplication - no errors

> x <- c(1,1) 
> m <- rbind(c(1,4),c(2,2))  
> m %*% x   # 1 
     [,1]
[1,]    5
[2,]    4
> x %*% m   # 2  
     [,1] [,2]
[1,]    3    6
> 

I can understand how and why the second multiplication works. In mathematics, a 1x2 matrix (1 row, 2 columns) can be multiplied by a 2x2 matrix.

But why does the first one work and why does it not cause any errors or warnings at all? In mathematics, a 2x2 matrix cannot be multiplied by a 1x2 matrix vector.

Note that if I initialize x, as shown below, and if I then multiply m by x, I get the same result as in the first example above.

> x <- cbind(c(1,1));
> x
     [,1]
[1,]    1
[2,]    1
> m %*% x   #  3  
     [,1]
[1,]    5
[2,]    4

So, I think this third example is the right way to do this.
Then why does the first example work fine without errors or warnings?

+4
source share
1 answer

?matmult:

matmult {base}

, . , , , . , ( ).

, ( ):

x %*% m
t(x) %*% m 
rbind(x) %*% m 

:

m %*% x
m %*% t(t(x))
m %*% cbind(x)

m %*% t(x) t(t(x)) %*% m , t() matrix, . , , -.

+1

Source: https://habr.com/ru/post/1620603/


All Articles