6

I have several processes, each computing its own matrix. Finally, I want them to send their solved matrices to the root process, which will literally join them into one big matrix and output this simply in ASCII format into a text file. Is it possible in some elegant way or do I have to iterate through every matrix and "join" it by myself?


Example:

matrix 0:
0 5 
4 5 

matrix 1:
1 2
3 4

matrix 2:
5 6
8 7

matrix 3:
0 0
2 1

Final "joined" matrix:
0 5 | 1 2
4 5 | 3 4
----+----
5 6 | 0 0
8 7 | 2 1
Community
  • 1
  • 1
Eenoku
  • 2,741
  • 4
  • 32
  • 64
  • 3
    This is explained in the [online documentation](http://arma.sourceforge.net/docs.html) for Armadillo. It's useful to read that before posting questions on Stackoverflow. Joining matrices is done using the [join_rows() and join_cols()](http://arma.sourceforge.net/docs.html#join) functions. – mtall Apr 04 '15 at 05:18
  • 1
    @mtall Please, rewrite your comment as the answer, it deserves to be accepted :-) – Eenoku May 04 '15 at 17:04

1 Answers1

6

Assuming your four matrix data is stored in data/a.txt,data/b.txt,data/c.txt,data/d.txt respectively, the following code would produce the desired result.

arma::mat a,b,c,d ;

a.load("data/a.txt");
b.load("data/b.txt");
c.load("data/c.txt");
d.load("data/d.txt");

// join rows (=horizontally, i.e. matrices must have the same number of rows)
auto joined_ab  = std::move(arma::join_rows( a, b ));
auto joined_cd  = std::move(arma::join_rows( c, d ));

// join columns (=vertically, i.e. matrices must have the same number of cols)
auto joined_mat = std::move(arma::join_cols( joined_ab, joined_cd ));

std::cout << joined_mat << std::endl;

Note: for the original question the following holds;

a = matrix 0

b = matrix 1

c = matrix 2

d = matrix 3

So the construction above yields:

a | b 
--+---
c | d
Pal
  • 989
  • 10
  • 23