r - How to select multiple cells in a matrix and perform an operation on corresponding cells in another matrix of the same size? -
i trying write r script pollution routing in world rivers, , need on selecting matrix cell coordinates , applying these other matrices of same dimension.
my data: have several matrices corresponding hydrological parameters of world rivers on half degree grid (360 rows, 720 columns). these matrices represent flow accumulation (how many cells flow cell), flow direction (which of 8 surrounding cells load of cell flow to) , pollutant load.
my idea: compute pollutant load in each grid cell start end of river. can base on flow accumulation (low high). however, each river basin can have multiple cells same flow accumulation value.
the problem: need select matrix cells of each value of flow accumulation (low high), find coordinates (row,column), , transfer corresponding pollutant load correct adjacent cell using flow direction matrix. have tried various ways, selecting coordinates of correct cells , applying these matrix cannot work.
i give example of have tried, using 2 loops on 1 single river basin. in example, flow direction value of 1 means pollutant load needs transferred adjacent cell right (row same, column +1):
basinflowaccumulation <-flowaccumulation[basin] basinflowaccumulationmaximum <- max(basinflowaccumulation) basinflowdirection <-flowdirection[basin] basinpollutant <-pollutant[basin] b<-0 for(i in 0:basinflowaccumulationmaximum){ cells.index<-which(basinflowaccumulation[]==b, arr.ind=true) (j in 1:length(cells.index)){ print(basinflowdirection[cells[j]]) row<-basinpollutant[cells[j[1]]] column<-basinpollutant[cells[j[2]]] ifelse(basinflowdirection[cells.index[j]]==1, basinpollutant[row,(column+1)]<-basinpollutant[row,(column+1)]+basinpollutant[row,column] } b<-b+1 }
any advice appreciated!
Comments
Post a Comment