Translating Matlab to Java

Hello, I’m trying to translate something from matlab to java for a project and I’m facing some (many) difficulties.
I have created a dummy block of code to explain what I’m trying to do atm. I tried several things including putColumn and I’m always failing. I also thought of iterating the column and from G (see the code below) and setting on Ge values using data() function, but I don’t like it so much and haven’t tested it yet.
Could you please help?


G = [1 2 3; 4 5 6; 7 8 9; 10 11 12];

for i=1:365

@officiallor could you try to lay this out a bit more clearly? A bit of context on what some of the matlab does would be useful as well. The less work we have to do to reverse engineer what you’re trying to do, the easier it is to help you. Try to explain what you’re trying to do with a complete code example.

1 Like

I’m not quite sure what it should produce tbh. I’m still waiting for some outputs from the guys that want it. I tried running it on octave-online dot net and the result is 1 column repeating the the G’s 1st column 365 times. So what I’m trying to do is append to a matrixe’s column (greater in size), one other matrixe’s column that is smaller in size and in general, how to manipulate matrixes using this lib. I have read the docs and tried several things but I was getting some exceptions regarding the size of the column.

In ND4J, tensors have a fixed size, as they have a fixed memory allocation.

You’ll need to create an appropriately sized matrix first, and then you can assign values to it. If you want to do that column by column, you can get a view into that matrix with indexing operations (i.e. arr.get(...) with indexes defined by NDArrayIndex) and then view.assign what you want to have in there.

@treo could you please provide an example for my problem? I’ve read the .java examples and most of the quick start documentation but still I’m not familiar with this.
Thank you
Edit: Note: I’d like something to understand and continue with this matlab project

I tried this

//G is a 96 rows table

            val Ge = Nd4j.create(96 * 365, 24) 

            for (i in 1..365) {
                Ge.assign(G.getAllRowsOfColumn(1)) //getAllRowsOfColumn body-->   
                                                  // return this.get(NDArrayIndex.all(), NDArrayIndex.point(columnIndex))


//Exception: Mis matched lengths: [840960] != [96] - Array 1 shape: [35040, 24], array 2 shape: [96, 1]

The answer is pretty clear, you can’t assign a 96-element array to a 840960-element array.

            val Ge = Nd4j.create(96, 365, 24) 

If you create your Ge tensor like that, you can access a view on that data in exactly the same way as you do with your G tensor and then assign to it.


Something like that should probably work.