Dear Susanne, Thank you for your answer :-) and for the other people that helped privately. I have been running the code with a friend, and we reached a similar conclusion. Matlab, apparently automatically transforms the matrices in vector and does the correlation between vectors thus obtaining one value only. This differs also from octave, which as R, do the correlation of the matrices*matrices. I managed to sort this out, transforming the matrices into vectors, simply by adding a c before.
We also found this aspect of 0-1 and 1-255 intriguing.In the end, I used a formula from a friend to do the transformation, getting a similar values in both programmes. Here's the code: ImageWidth = dim(x)[2] #number of col MaxOffset = 99; #defined variable ImageWidthToProcess = ImageWidth-MaxOffset; #col-defined variable AutoCData=0 #dif from matlab: in R we should create the matrix/dataframe where we will store the data created by the loop for(Offset in 1:MaxOffset){ #Offset=2 OffsetPlaquette = x[,c(1+Offset):c(ImageWidthToProcess+Offset)] AutoCData[Offset] = cor(c(x[,1:ImageWidthToProcess]), c(OffsetPlaquette)) print(Offset) } AutoCData plot(AutoCData) ### COOL :-) The results were very similar to matlab. I still have many lines to go :-) Using the function you very well produced (thank you so much), the difference between the results of the two are low summary(AutoCData2-AutoCData) Min. 1st Qu. Median Mean 3rd Qu. -2.581e-15 -2.741e-16 -2.082e-17 5.723e-17 2.637e-16 Max. 5.329e-15 which is good! All the best, Marta 2010/3/29 Susanne Schmidt <s.schm...@bham.ac.uk> > Dear Marta, > > I did it in Matlab, and fiddled around with R code until I had *almost* the > same result. The "almost" is probably due to R handling the picture values > (ranging from 0 to 1) differently than Matlab (ranging from 0 to 255), and > simply multiplying the R picture values by 255 did NOT result in exactly the > same values as the Matlab values. [what seems white in the picture is 245 in > Matlab, although values potentially range to 255, and white is 0.9642549 in > R, which multiplied by 255 gives 245.12, e.g.] > But maybe the precision of this solution is good enough for you .. > > The corr2 demand from matlab is a 2D correlation coefficient - the R > command cor works elementwise, and is not the solution here. > Below I tried to implement the formula given in the following matlab page: > http://www.mathworks.com/access/helpdesk/help/toolbox/images/corr2.html > > Maybe somebody on the list has a nice idea how to make the code more > elegant > > This is the complete code in R > > setwd("D:/ wherever ") > library(ReadImages) > x <- read.jpeg(" whichever .jpg") #open image > > plot(x) #plot image > x <- rgb2grey(x) #convert to greyscale > plot(x) # check ;-) the image is in grey scale > > ImageWidth = dim(x)[2] #number of col > MaxOffset = 99; #defined variable > ImageWidthToProcess = ImageWidth-MaxOffset; #col-defined variable > > ## this one does NOT work because matrices not square: > for(k in 1: MaxOffset) { > OffsetPlaquette <- x[ , c((1+ k) : (ImageWidthToProcess + k))] > dataToProcess <- x[,c(1:ImageWidthToProcess)] > AutoCData[k] <- mantel(OffsetPlaquette, dataToProcess) > } > AutoCData > ## END this one does not work because matrices not square > > > AutoCData <- rep(0, MaxOffset) > sumBothM <- rep(0, MaxOffset) > sum1stMsq <- rep(0, MaxOffset) > sum2ndMsq <- rep(0, MaxOffset) > for(k in 1: MaxOffset) { > OffsetPlaquette <- x[ ,(1+k) : (ImageWidthToProcess + k)] > dataToProcess <- x[,c(1:ImageWidthToProcess)] > meanM <- mean(OffsetPlaquette); meanM2 <- mean(dataToProcess) > for(j in 1:dim(dataToProcess)[2]){ > for(i in 1:dim(OffsetPlaquette)[1]){ > sumBothM[k] <- sumBothM[k] + > (OffsetPlaquette[i,j]-meanM)*(dataToProcess[i,j]-meanM2) > sum1stMsq[k] <- sum1stMsq[k] + (OffsetPlaquette[i,j]-meanM)^2 > sum2ndMsq[k] <- sum2ndMsq[k] + (dataToProcess[i,j]-meanM2)^2 > } > } > AutoCData[k] <- sumBothM[k]/(sqrt(sum1stMsq[k] * sum2ndMsq[k])) > } > > AutoCData > > > Best wishes, > Susanne > [[alternative HTML version deleted]] ______________________________________________ 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.