[R] filling a matrix who's entries are a function of the ind

Jonathan Baron baron at psych.upenn.edu
Sat Aug 23 23:44:18 CEST 2003


On 08/23/03 22:25, Ted Harding wrote:
>On 23-Aug-03 Douglas G. Scofield wrote:
>> What's the best way in R to fill a matrix who's entries depend on some
>> function of the indices?  I'm currently doing:
>> 
>>    Q <- matrix(0, k, k)
>>    for (A in 1:k) {
>>       for (B in 1:k) {
>>          Q[A,B] <- my.function(A,B)
>>       }
>>    }
>> 
>> but I wonder if there is a more terse way.

outer(1:k,1:k,my.function)

-- 
Jonathan Baron, Professor of Psychology, University of Pennsylvania
Home page:            http://www.sas.upenn.edu/~baron
R page:               http://finzi.psych.upenn.edu/
deleting "Your details" "Your application" "Approved" "Thank you!"




More information about the R-help mailing list