# 2D array workaround?

Hi,
I have 6 different arrays (CIELAB coordinates) and an equation (Euclidean Distance) linking them all together:
Lref[j], aref[j], bref[j] (GROUP 1) - Fixed length [213] (reference values).
L[i], a[i], b[i] (GROUP 2) - Variable length (experimental values).
EucDist = sqrt(pow(L[i]-Lref[j], 2) + pow(a[i]-aref[j], 2) + pow(b[i]-bref[j], 2));

I need to generate a table/matrix (XY = X213) with X columns and the result of “EucDist” in each cell of the 213 rows of each column. Is there anyway for doing this? I’ve tried loops with two variables (i, j), loops within loops, etc. but I can’t get this… I guess I need 2D arrays and I’m aware ImageJ doesn’t allow this. Is there any workaround for this case?

I attached an Example in Excel.
Example.zip (13.3 KB)

Thanks.

Lee

Hey @insertsk8,

do you want to share the code you tried? It might then be easier for us to point you in the right direction

Cheers,
Robert

2 Likes

Hello Lee -

The ImageJ Macro language (IJM) doesn’t support 2D arrays, but
ImageJ itself does.

The best “workaround” would be to switch to jython (python) or one
of the other scripting languages supported by ImageJ (or use java,
the language in which ImageJ is written).

If you wish to stick with IJM, there are  three common workarounds:

 Zero: Use “index arithmetic” to pack a 2D array into a 1D array.
Thus:

``````for (i = 0; i < nRows; i++) {
for (j = 0; j < nColumns; j++) {
matrix_entry_ij = matrix[i * nColumns + j];
//  process matrix_entry_ij
}
}
``````

This is an old-school multi-dimensional matrix idiom.

First: Use a bunch of row variables, each of which is an IJM array
containing the fields in that row, and access them with a big nested
if statement. Thus: `row1`, `row2`, …, `row213`, and

``````for (i = 1; i <= 213; i++) {
if  (i == 1)   // process row1
else if  (i == 2)   // process row2
...
else if  (i == 213)   // process row213
}
``````

Of course, this will be rather unwieldy, given the size of your matrix.

Second: Pack one of your dimensions into strings that you then
parse. Thus, `matrix` is an IJM (1D) array, and

``````matrix[0] = "1.1, 1.2, ..., 9.9";
...
for (i = 0; i < lengthOf (matrix); i++) {
vals = split (matrix[i], ",");
for (j = 0; j < lengthOf (vals); j++) {
matrix_entry_ij = vals[j]
//  process matrix_entry_ij
}
}
``````

This is also somewhat annoying, but works.

( You can also use the Results Table as a 2D array, but it has
named columns, rather than indexed columns, so you’re not really
any better off than using a bunch of named row (or named column)
variables.)

My recommendation would be jython, rather than one of these
workarounds.

Thanks, mm

2 Likes

This helped me a lot!
I also tried slicing the 1D array depending on the number of columns I have.
Printing the array and saving it in .csv, and then opening the file again and splitting it in rows and the delimiter “,” allowed me to get a pseudo 2D result table.

``````Array.print(val1);
for(i = 1; i<codeL; i++){
initial = i*ExpL;
final = (i+1)*ExpL;
val2 = Array.slice(num0,initial,final);
Array.print(val2);
}
``````

Thanks!
Lee