matlab - Scaling Up A Matrix in R -
i'm working on project involves porting matlab code r, , have run bit of problem 1 chunk of code.
what piece of code in matlab resizes mask (just 1s , 0s) same sizes much larger set of info masking. info mask applied changes in size, can't set static values scaling.
the matlab code has function resizem resizes mask, preserving 1s , 0s characteristic.
i've searched (to no avail) solutions problem, don't think issue plagues many r users (it's non-traditional utilize of r). wondering if knew way resize matrix in same way resizem does.
for example:
if had array
[1,0,1,1,1, 0,0,0,1,1, 1,1,0,0,1]
and wanted scale 3x5 7x10 7x10 matrix containing 1s , 0s this.
1 1 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1
how approach r function.
here's 1 possible r implementation of such function
resizem <- function(m, rows,cols) { rs <- round(seq(0, rows-1)/(rows-1) * (nrow(m)-1) +1) cs <- round(seq(0, cols-1)/(cols-1) * (ncol(m)-1) +1) m[rs, ][, cs] }
here require specify number of rows , cols want in new matrix. utilize default nearest neighbour interpolation (resizem has other options). seems work test data
m <- matrix(c(1,0,1,1,1, 0,0,0,1,1, 1,1,0,0,1), byrow=t, nrow=3) resizem(m, 7,10)
and returns
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 1 0 0 1 1 1 1 1 1 [2,] 1 1 0 0 1 1 1 1 1 1 [3,] 0 0 0 0 0 0 1 1 1 1 [4,] 0 0 0 0 0 0 1 1 1 1 [5,] 0 0 0 0 0 0 1 1 1 1 [6,] 1 1 1 1 0 0 0 0 1 1 [7,] 1 1 1 1 0 0 0 0 1 1
r matlab matrix resize
No comments:
Post a Comment