Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I join two Matrix in Java

Tags:

java

join

matrix

I want to join 2 matrix with the same number of columns and different number of lines, but I'm wondering how can I do this with one command.

I already know how to do this using for's, then, I want to know if there is a command in Java that do the job for me.

For example

int m1[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

int m2[][] = {{10, 11, 12}, {13, 14, 15}};

Magic command to join them into a matrix m

int m = join(m1,m2);

m = 

1 2 3

4 5 6

7 8 9

10 11 12

13 14 15
like image 342
marionmaiden Avatar asked Dec 05 '25 10:12

marionmaiden


1 Answers

int m[][] = new int[m1.length+m2.length][];
System.arraycopy(m1, 0, m, 0, m1.length);
System.arraycopy(m2, 0, m, m1.length, m2.length);

You might want to clone each line though

like image 162
Maurice Perry Avatar answered Dec 07 '25 04:12

Maurice Perry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!