using multiarrays to fill a 3 plane matrix

efe's icon

Hello!
I am trying to fill with random values a 3 plane matrix. I would like to a

I am try to fill with random values a 3 plane matrix. I have declared a multidimensional array with the idea of filling the planes with each one of the dimensions of the array(0,1,2). The class looks like this:

import com.cycling74.max.*;
import com.cycling74.jitter.*;
import java.util.Random;

public class V6 extends MaxObject{
int x=3;
int y=3;
int size=x*y;
private int[][][][] intArr=new int[size][size][size][3];
Random myRandom=new Random();
JitterMatrix mat9=new JitterMatrix("fff",3,"float32",x,y);

public V6(Atom[] args){
declareInlets(new int[]{DataTypes.ALL});
}

public void bang(){
//no funciona porque hay que llenar cada uno de los planos
for(int i=0;i
for(int j=0;j
for(int k=0;k
intArr[i][j][k][0]=myRandom.nextInt(300);
intArr[i][j][k][1]=myRandom.nextInt(300);
intArr[i][j][k][2]=myRandom.nextInt(300);
}
}
}

mat9.copyArrayToMatrix(intArr);

outlet(0,"jit_matrix",new Atom[]{Atom.newAtom(mat9.getName())});
}
}

However, I find that the copyArrayToMatrix method is not working with the multidimensional array. My idea is not to use this type of approach to generate random values but to use it for automatons; i know the best answer would be to use a matrixcalc with noise, but i would like to sort out how to link the members of the array to specific planes. What am i missing here?

Thank you!
Emmanuel

Joshua Kit Clayton's icon

It only supports flat arrays. Like the following:

private int[] intArr=new int[size*size*size*3];

// i is the z dimension, j is the y dimension, k is the x dimension
index =i*size*size+ j*size + k*3;
intArr[index]=myRandom.nextInt(300);
intArr[index+1]=myRandom.nextInt(300);
intArr[index+2]=myRandom.nextInt(300);

I'm not positive that array to matrix works for higher than 2 dimensions, in which case, you'll need to use the array to vector methods.

efe's icon

Hello Joshua. Thank you for the fast answer.
I will test the different methods provided by the API and see which one is the best option for having the data stored. I need to admit that i am quite surprised multi arrays cannot be mapped into matrix planes directly. Maybe a good feature for future jitter versions!
I would be interested in knowing your opinion(and anyone else of course):
For the project i am working on I really need to use multi-arrays for storing data for controlling some external devices. My first idea was to design the system using mostly java but now I am wondering if this kind of tasks can be handled better using c externals. Becaise i am not sure if i will face the same limitation as here i would be interested in getting some feedback on the topic.
Thank you!
Emmanuel

Joshua Kit Clayton's icon

I believe that accessing multi-arrays would be slower across the Java C boundary than a flat array.

Whether you use C or Java, ultimately a jitter matrix has to be flattened memory, so I wouldn't see this as a reason to switch to C. If you look at all the C examples, you'll see that we calculate our index offsets in a similar fashion to what I describe above. Ultimately that's what a multi-array is doing for you, so you shouldn't have to worry about the cost of the index =i*size*size+ j*size + k*3 calcuation.

If it's easier for your code, you can copy your multi-array to a flat array just before calling the copyArrayToMatrix method.

-Joshua

efe's icon

Thank you Joshua, as always your help is very useful.

pgk's icon

Hey Emmanuel!!

I think what Joshua said (the flattened approach) is the way to go!

A multidimentional array[][][] is an array of arrays of arrays, so at least imho it is really easier to think of it in terms of offsets by size (kinda related to pointer arithmetic)!
Might I also suggest the JAMA package, a java package for dealing with matrices in the mathematical sense (solving equations and whatnot), by the guys from wolfram. Search the net! It even lets you export the matrix as a flat array.
Only deals with doubles, but you can modify it to deal with floats etc.

Cheers,
Panos