kostiamol
kostiamol

Reputation: 309

Matlab '* operation to R

I have two vectors A and B in Matlab and I perform such an operation:

A = [1, 3, 2];
B = [2, 1, 3];
C = A'*B;

As a result C equals:

2     1     3
6     3     9
4     2     6

But I have no idea how to translate that into R. I tried this construction, but the result is different:

C <- Conj(t(A)) %*% B

Upvotes: 0

Views: 34

Answers (1)

Lamia
Lamia

Reputation: 3875

By default in R, when creating a matrix from a vector of length n, you get a matrix with n rows and one column.

A = matrix(c(1, 3, 2));
 [,1]
[1,]    1
[2,]    3
[3,]    2

B = matrix(c(2, 1, 3));
[,1]
[1,]    2
[2,]    1
[3,]    3

C = A %*% t(B)

Which returns:

 [,1] [,2] [,3]
[1,]    2    1    3
[2,]    6    3    9
[3,]    4    2    6

In case you want A and B to have 3 columns and 1 row, do:

A = matrix(c(1, 3, 2),ncol=3);
B = matrix(c(2, 1, 3),ncol=3);
C = t(A) %*% B

Which returns the same result.

Upvotes: 1

Related Questions