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

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. :)

有帮助吗?

解决方案

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

?`%*%`
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top