R how to do a for loop and multiply each row in a specific matrix to a column (and they display results)?

StackOverflow https://stackoverflow.com/questions/22826451

Question

Let's say I have matrix a which is 10x10 (10 rows) and matrix b which is 10x1 (column)

and I want to multiply each row of a with b, so the end result would be a column of 10 elements.

for(i in 1:nrow(a)) a[i,] %*% b[,1]

I have something like the above right now

1) Is this correct? 2) How do I get it to display on screen?

Thank you, please keep in mind I'm a beginner with R. :)

Was it helpful?

Solution

Close! In this case, you can simply do

c <- a %*% b

The operator %*% is actual matrix multiplication. For more on this consult the help page using

?`%*%`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top