# Translating Matlab to Java

**URL:** <https://community.konduit.ai/t/translating-matlab-to-java/1486>\
**Category:** ND4J\
**Created:** [July 8, 2021, 6:34pm UTC](https://community.konduit.ai/t/translating-matlab-to-java/1486 "2021-07-08T18:34:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![officiallor](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/officiallor/32/596_2.png) [@officiallor](https://community.konduit.ai/u/officiallor)\
**Post date:** [July 8, 2021, 6:34pm UTC](https://community.konduit.ai/t/translating-matlab-to-java/1486/1 "2021-07-08T18:34:45Z")

</div>

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?

Code:

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

for i=1:365  
Ge=[Ge;G(:,1)];  
end

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [July 8, 2021, 9:44pm UTC](https://community.konduit.ai/t/translating-matlab-to-java/1486/2 "2021-07-08T21:44:17Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![officiallor](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/officiallor/32/596_2.png) [@officiallor](https://community.konduit.ai/u/officiallor)\
**Post date:** [July 9, 2021, 6:38am UTC](https://community.konduit.ai/t/translating-matlab-to-java/1486/3 "2021-07-09T06:38:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [July 9, 2021, 6:48am UTC](https://community.konduit.ai/t/translating-matlab-to-java/1486/4 "2021-07-09T06:48:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![officiallor](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/officiallor/32/596_2.png) [@officiallor](https://community.konduit.ai/u/officiallor)\
**Post date:** [July 9, 2021, 7:19am UTC](https://community.konduit.ai/t/translating-matlab-to-java/1486/5 "2021-07-09T07:19:28Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![officiallor](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/officiallor/32/596_2.png) [@officiallor](https://community.konduit.ai/u/officiallor)\
**Post date:** [July 9, 2021, 7:56am UTC](https://community.konduit.ai/t/translating-matlab-to-java/1486/6 "2021-07-09T07:56:12Z")

</div>

I tried this

//G is a 96 rows table

```auto
            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]

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [July 12, 2021, 6:00am UTC](https://community.konduit.ai/t/translating-matlab-to-java/1486/7 "2021-07-12T06:00:02Z")

</div>

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

```auto
            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.

```auto
Ge.getAllRowsOfColumn(i).assign(G.getAllRowsOfColumn(1))

```

Something like that should probably work.
