1

Having the following code in Matlab:

r=  zeros(10,10);
r(:) = x(z);

given that "x" is a single dimension array and "z" is a 2d array what is the function r(:) = x(z); doing and how could I implement something similar in Java?

2
  • what do you need? an array containing a 2d array? Please be clearer and you will get your answer quickly. :) Commented Dec 6, 2011 at 15:53
  • well first I would need somebody to explain what x(z); does as I found some code in Matlab and want to implement it in Java but I don't know what it is exactly doing. Commented Dec 6, 2011 at 15:58

2 Answers 2

4

Firstly, x(z) takes each element in the 2d-array z, and uses it as an index to fetch data from x.

For example x=[1 2] z = [1 1; 1 2] would give the result x(z) = [1 1; 1 2] because x(z) is really [x(z(1,1)) x(z(1,2)); x(z(2,1)) x(z(2,2))]. Therefore you have to be careful that the values in z doesn't exceed the size of x or you'll get index out of bounds.

r(:) = x(z) is basically the same as r = x(z).

To implement this in java you would need som kind of for-loop over the elements in z. In this loop you would construct a 2d-array by assigning the current index (i,j) with the value in x(z(i,j)).

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

So the java code looks as follows:

for (int i = 0; i < z.length; i++) {
             for (int j = 0; j < z[0].length; j++) {
                y[i][j] = x[z[i][j]];
            }

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.