Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB - re-arrange matrix by vertically concatenating submatrices

Tags:

matrix

matlab

I am having trouble with the following task: Suppose a 3x6 matrix:

A =

0.2787    0.2948    0.4635    0.8388    0.0627    0.0435
0.6917    0.1185    0.3660    0.1867    0.2383    0.7577
0.6179    0.7425    0.0448    0.4009    0.9377    0.4821

What I would like to do is to divide the matrix into blocks, like this:

A =

0.2787    0.2948  |  0.4635    0.8388  |  0.0627    0.0435
0.6917    0.1185  |  0.3660    0.1867  |  0.2383    0.7577
0.6179    0.7425  |  0.0448    0.4009  |  0.9377    0.4821

and vertically concatenate these blocks to get the final result:

0.2787    0.2948 
0.6917    0.1185  
0.6179    0.7425  
0.4635    0.8388
0.3660    0.1867
0.0448    0.4009
0.0627    0.0435
0.2383    0.7577
0.9377    0.4821

I think if I can get help with this, then I can perhaps do it for arbitrary matrices A. I can solve the above problem using for-loops, but I am looking for a vectorised solution.

Thanks in advance! N.

like image 236
user1438310 Avatar asked Jan 16 '23 11:01

user1438310


1 Answers

Consider the following:

A = rand(3,6);
blkSz = 2;

C = mat2cell(A, size(A,1), blkSz*ones(1,size(A,2)/blkSz));
C = cat(1,C{:})

This assumes that size(A,2) is evenly divisible by blkSz

like image 111
Amro Avatar answered Feb 17 '23 20:02

Amro