Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join matrices in Armadillo

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
like image 274
Eenoku Avatar asked Apr 03 '15 16:04

Eenoku


1 Answers

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
like image 79
Pal Avatar answered Nov 09 '22 13:11

Pal