Compute Mean square error between each pair of 31 arrays on imagej or excel

I want to compute the Mean Square Error between each pair of 31 arrays of size 1024 containing gray values of profiles of lines on 31 images.

My arrays are full_yval1 to full_yval31 and i want to find out a way on imagej to code this on a macro and to get à 2D matrix (31by31) or an array of 961 (31*31). i tried :

MSE = newArray(961);
for (i = 1; i < 32; i++) {
    for (j = i; j < 32; j++) {
        for (k = 0; k < full_yval1.length; k++) {
            MSE[i][j] = sqrt(pow(full_yvali[k] - full_yvalj[k],2)/full_yval1.length);

but this is not functiooning in Imagej.

I also have the csv file of results but i dont know how to do this on excel too.

Can somebody help me please.

Thank you.

Hello Salem -

There are two related issues:

First, the IJM macro language does not support two-dimensional
(or multi-dimensional) arrays or arrays of arrays. So MSE[i][j]
won’t work. You could have MSE be a one-dimensional array, and
index into it “by hand”:

  MSE [31 * i + j] = ...;

Second, full_yvali and full_yvalj are names of variables
(that you haven’t defined). Just because some other variable, i,
happens to have a value of, say, 3, doesn’t make full_yvali
turn into the (defined) variable full_yval3.

(If IJM supported arrays of arrays, this would be a natural time
to have something like full_yval_all be an array of 31 arrays,
with, say, full_yval_all[3] being the array full_yval3.
But, again, IJM doesn’t support arrays of arrays.)

Your best bet – for both of these issues – would be to move to a
scripting language such as jython (python) or javascript that does
support arrays of arrays (in jython you would probably use a list
of lists).

Thanks, mm

Rather than putting the arrays of profile values into arrays, you could transfer your profile values into an image as 8-, 16- or float (32-bit) pixel values and address the pixels as a two-dimensional array and put the resulting calculation in another window, addressed the same way.

Hello mountain_man
i want to thank you for your response, in fact i already know that the variables full_yvali and full_yvalj are undefined but i was looking for a way to iterate on them but apprently there is no way to do this.
I have do the job and got what i want with Python using Pandas dataframes and Numpy arrays.
Thank you again.

Best regards.

Hi eljonco,

I dont understand what you mean, how can i make a profile (1d array) into a 2D image ?
I have a profile of 1024 points : would i create a 1024*1 pixels image ? So 31 images?

The following macro opens the sample image Boats, draws a line, shows a profile and obtains the values of that profile, then puts the profile values in pixels of a 16-bit image. You can now address each value using as if you have an array named MathArray[x,y]; by getting selecting the mathArray image, then get the pixel value from [x,y]. That would code to getPixel(x,y);. I’ve encoded all 32 rows with the same profile, you would put each profile in a separate row.
Maybe you have requirements for larger numbers than 65535, then you would create a float image rather than a 16-bit image using newImage("MathArray", "32-bit black", 1024, 32, 1);.

run("Clear Results");
run("Boats (356K)");
makeLine(30, 275, 678, 261);
run("Plot Profile");//only for visualisation
newImage("MathArray", "16-bit black", 1024, 32, 1);
getDimensions(width, height, channels, slices, frames);
profile = getProfile();
	for (x=0; x<profile.length; x++){
		profileValue=profile[x];//for debugging visibility
		setPixel(x, y, profileValue);
run("Enhance Contrast", "saturated=0.35");
1 Like

Thank you very mouch.