The following code gives you functionality close to what you are looking for
(I think)... but someone more familar with proto might be able to make it
sexier. You will need to install the package 'proto' (though with a bit of
work you might be able to do this without proto, using environments alone.




library(proto)
my.mat <- function(mat) {
    structure(proto( expr = {
        mat <- mat
        set.vals <- function(., rs, cs, vals) {
            if(max(rs) > nrow(.$mat) || min(rs) < 1) stop("row out of
range")
            if(max(cs) > ncol(.$mat) || min(cs) < 1) stop("column out of
range")
            .$mat[rs, cs] <- vals
        }
        get.mat.ref <- function(., rows, cols) {
            if(max(rows) > nrow(.$mat) || min(rows) < 1) stop("row out of
range")
            if(max(cols) > ncol(.$mat) || min(cols) < 1) stop("column out of
range")
            tmp <- proto(., expr = {
                set.vals <- function(., rs, cs, vals) {
                    if(max(rs) > length(.$rows) || min(rs) < 1) stop("row
out of range")
                    if(max(cs) > length(.$cols) || min(cs) < 1) stop("column
out of range")
                    .super$mat[.$rows[rs], .$cols[cs]] <- vals
                }
            })
            tmp$rows <- rows
            tmp$cols <- cols
            structure(tmp, class=c('my.sub.mat', 'proto', 'environment'))
        }
    }), class=c('my.mat', 'proto', 'environment'))
}

"[.my.mat" <- function(x, ...) get('mat', x)[...]
"[<-.my.sub.mat" <- "[<-.my.mat" <- function(x, i, j, value) {x$set.vals(i,
j, value); as.proto(x)}
print.my.sub.mat <- function(x) print(get('mat',x)[get('rows', x),
get('cols', x)])
print.my.mat <- function(x) print(get('mat',x))




#You can use this code like:

a <- my.mat(matrix(1:(4*3),4,3))
b <- a$get.mat.ref(2:3, 2:3)
b[1,1] <- 42  # equivalent to b$set.vals(1,1,42)
b[1:2,2] <- 43
a[1,1] <- 44
a[rep(4, 2), 2:3] <- 45
b # print the sub matrix
a # print the matrix

#test passing sub-matrix to a function
test <- function(x) x[2,1] <- 46
test(b)
a # print the matrix again (it is as we would like)

# extract the original matrix
dat <- a$mat




Hope this helps,
Simon Knapp





On Mon, Jan 18, 2010 at 12:36 PM, Friedrich <gotisch@gmail.com> wrote:

> Hello,
>
> I'm am in the process of trying to port a RATS functions to R and have
> the problem, that RATS allows for the creation of submatrixes that are
> linked to their basematrix.
>
> Basically it should work like this:
>
> a = matrix(1:(4*3),4,3)
> a
> #      [,1] [,2] [,3]
> # [1,]    1    5    9
> # [2,]    2    6   10
> # [3,]    3    7   11
> # [4,]    4    8   12
>
> # This of course doesnt work :)
> b = submatrix(a,fromx=1,tox=2,fromy=1,toy=2)
> b
> #      [,1] [,2]
> # [1,]    0   10
> # [2,]    7   11
> b[1,1] = 42
> a[1,1]
> # [1] 42
>
> so changes in the sub-matrix propagate to the "base" matrix.
>
> Does such a feature exist?
>
> Thanks,
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]

