[R] place values into a matrix efficiently?

Carl Witthoft carl at witthoft.com
Sun Nov 20 04:27:43 CET 2011


This question attacked me as I was thinking about matrix value updates.

I probably will never need to do this, but wanted to ask if there are 
efficient methods to perform the for-loop in the following sequence.

%xymat<-matrix(rep(0,100) nr=10,nc=10)  # empty matrix
%x<-1:10
%y<-sample.int(10,10,rep=T)

%for (j in 1:10) xymat[x[j],y[j]] <- some_function(x[j],y[j]) #to create 
either false-color or 3D map .
plot(0:1,0:1,t='n')
% rasterImage(xymat/max(xymat),0,0,1,1,interp=F)

This will produce a raster image of the original data(x vs y) that looks 
like plot(x,y) .

Anyway, I just seem to be blanking: is there some vectorized way to 
place values, or even a constant value, into all elements of xymat whose 
row,col coordinates match ordered pairs in x and y ?

Carl
-- 

Sent from my Cray XK6
"Pendeo-navem mei anguillae plena est."



More information about the R-help mailing list