[R] Display of 3d arrays
Agustin Lobo
alobo at ija.csic.es
Tue Aug 28 07:55:44 CEST 2001
Thanks Ben,
An improvement would be indexing the colors,
as often there will be fewer colors than cells.
For example:
tstarr <-
array(rep(c(0,0,0,0,200,200,200,200,250,250,250,250),12*3*3),dim=c(8,3,3))
colmat <- apply(tstarr/256,c(1,2),function(z)do.call("rgb",as.list(z)))
cols <- as.vector(unique(colmat))
^^^^^^
and use (for this example):
cbind(c(rep(1,4),rep(2,4)),c(rep(3,4),rep(1,4)),c(rep(2,4),rep(3,4)))
in image() instead of:
matrix(1:length(colmat),nrow=nrow(colmat))
The question now is to find a general way of selecting and
indexing the unique triplets.
Agus
Dr. Agustin Lobo
Instituto de Ciencias de la Tierra (CSIC)
Lluis Sole Sabaris s/n
08028 Barcelona SPAIN
tel 34 93409 5410
fax 34 93411 0012
alobo at ija.csic.es
On Mon, 27 Aug 2001 ben at zoo.ufl.edu wrote:
>
> This isn't quite a perfect solution but you might be able to make it
> work:
>
> ## data
> x <- c(170,174,173,172,161,
> 171,178,174,166,149,
> 168,174,173,166,156,
> 171,170,173,166,164,
> 167,170,170,171,169,
> 138,131,128,128,125,
> 138,127,129,122,134,
> 144,131,130,127,132,
> 142,138,130,131,129,
> 146,141,135,129,131,
> 124,123,127,125,125,
> 125,126,126,126,123,
> 127,123,125,128,128,
> 123,121,125,125,126)
>
> ## reshape data to your specifications
> y <- array(x,dim=c(5,5,3))
> z <- aperm(y,c(2,1,3))
>
> ## color matrix from array
> colmat <- apply(z/256,c(1,2),function(z)do.call("rgb",as.list(z)))
>
>
> ## hack -- fake image() into plotting specific colors
> cols <- as.vector(colmat)
> image(matrix(1:length(colmat),nrow=nrow(colmat)),col=cols)
>
> ## test
> tstarr <- array(c(256,256,0,0,0,0,256,256,0,256,256,0),
> dim=c(2,2,3))
> tstarr
> colmat <- apply(tstarr/256,c(1,2),function(z)do.call("rgb",as.list(z)))
> cols <- as.vector(colmat)
> image(matrix(1:length(colmat),nrow=nrow(colmat)),col=cols)
>
>
> On Sun, 26 Aug 2001, Agustin Lobo wrote:
>
> > Hi!
> >
> > Is it possible to display a 3d array as
> > an RGB image? For example, given the following
> > array:
> > > matriz3d
> > , , 1
> >
> > [,1] [,2] [,3] [,4] [,5]
> > [1,] 170 174 173 172 161
> > [2,] 171 178 174 166 149
> > [3,] 168 174 173 166 156
> > [4,] 171 170 173 166 164
> > [5,] 167 170 170 171 169
> >
> > , , 2
> >
> > [,1] [,2] [,3] [,4] [,5]
> > [1,] 138 131 128 128 125
> > [2,] 138 127 129 122 134
> > [3,] 144 131 130 127 132
> > [4,] 142 138 130 131 129
> > [5,] 146 141 135 129 131
> >
> > , , 3
> >
> > [,1] [,2] [,3] [,4] [,5]
> > [1,] 124 123 127 125 125
> > [2,] 125 126 126 126 123
> > [3,] 127 123 125 128 128
> > [4,] 123 121 125 125 126
> > [5,] 118 122 124 126 125
> >
> > the first row would be diplayed with rgb colors matriz3d[1,1:5,]
> >
> > (170 138 124) (174 131 123) (173 128 127) (172 128 125) (161 125 125)
> >
> >
> > Thanks
> >
> > Agus
> >
> > Dr. Agustin Lobo
> > Instituto de Ciencias de la Tierra (CSIC)
> > Lluis Sole Sabaris s/n
> > 08028 Barcelona SPAIN
> > tel 34 93409 5410
> > fax 34 93411 0012
> > alobo at ija.csic.es
> >
> >
> > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> > Send "info", "help", or "[un]subscribe"
> > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
> > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> >
>
> --
> 318 Carr Hall bolker at zoo.ufl.edu
> Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker
> Box 118525 (ph) 352-392-5697
> Gainesville, FL 32611-8525 (fax) 352-392-3704
>
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list