kdino
kdino

Reputation: 47

Making matrix in R

I want to make matrices without using loops such as for , while. So I tried assigned k and put k in function which makes matrices.

powlist= function(base,startnum,endnum) (base)^(startnum:endnum)

m_maker= function(base) matrix(c(powlist(base,0,19)),4,5)

k= 2:10
a= m_maker((k-1)/k)

But function returns only one matrix. I think function should return 9 matrices. Please let me know how should I change this code.

I want to make each matrices that first one is matrix m_maker(1/2) and second one m_maker(2/3) so on. When I put k=2 and k=3 each time, it returns what I want. What I want is way to return 9 matrices at one to go.

Upvotes: 0

Views: 63

Answers (1)

Frank
Frank

Reputation: 66819

You're looking for lapply, like

res <- lapply((k-1)/k, m_maker)

However, you really should use an array for something like this.

ares <- abind(res, along=3)

Upvotes: 1

Related Questions