I want to go from something like this:
1> a = matrix(c(1,4,2,5,2,5,2,1,4,4,3,2,1,6,7,4),4)
1> a
[,1] [,2] [,3] [,4]
[1,] 1 2 4 1
[2,] 4 5 4 6
[3,] 2 2 3 7
[4,] 5 1 2 4
To something like this:
[,1] [,2]
[1,] 12 15
[2,] 10 16
...without using for-loops, plyr, or otherwise without looping. Possible? I'm trying to shrink a geographic lat/long dataset from 5 arc-minutes to half-degree, and I've got an ascii grid. A little function where I specify blocksize would be great. I've got hundreds of such files, so things that allow me to do it quickly without parallelization/supercomputers would be much appreciated.
I guess that might help you, but still it uses sapply which can be considered as loop-ish tool.
Is it anyhow helpful ?
I used
1L
and2L
instead of1
and2
so indices remain integers (as opposed to numerics) and it should run faster that way.You can use matrix multiplication for this.
Square-matrix example, uses a matrix and its transpose on each side of
a
:Non-square example: