MATLAB Answers

How to create matrix with other matrixes by joining them?

34 views (last 30 days)
I'm trying to construct a matrix from the elements of several vectors, by joining them, For example:
a = [1 2 3];
b = [4 5 6];
c = [7 8 9];
d = [10 11 12];
And it should result in:
res =
1 4 2 5 3 6
7 10 8 11 9 12
Which is:
res =
a(1) b(1) a(2) b(2) a(3) b(3)
c(1) d(1) c(2) d(2) c(3) d(3)
I would need a method that can do this for any number of vectors with the same length.
Using a for loop and clever indexing would propably be one way, but I'm looking for a solution without using loops.


Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 11 Jan 2021
a = [1 2 3];
b = [4 5 6];
c = [7 8 9];
d = [10 11 12];
res = reshape([a;c;b;d],2,[])
res = 2×6
1 4 2 5 3 6 7 10 8 11 9 12


Show 1 older comment
Stephen Cobeldick
Stephen Cobeldick on 11 Jan 2021
"Could you tell me how to generalise this to more vectors?
Probably, once you explain how the data from "more vectors" should be arranged, e.g.:
  • how many output rows? Always 2? Or sqrt of the number of input vectors? Or something else?
  • how many input vectors, e.g. always an even number? Or a number that is a multiple of no. of rows?
  • what is the required sequence?
"What is the pattern or logic here?"
MATLAB stores all arrays in the same way: down the columns, then along the rows, them for each page, etc.:
So all you need to do is arrange the data in one array so that the required output elements are arranged in that order. Then one reshape give what you want.
For my answer I hard-coded the element order. In the general case you would use permute.
Csanad Levente Balogh
Csanad Levente Balogh on 11 Jan 2021
Is it possible to do that to any number of vetors in any order? So let's say you have more vectors, and a given arrangement, for example:
a, b, c, d, e, f, g, h, i, j, k, l <-- vectors
arrangement = [3 4] % meaning you want to make a series of 3x4 matrixes from the elements
a(1) b(1) c(1) d(1) a(2) b(2) c(2) d(2)
e(1) f(1) g(1) h(1) e(2) f(2) g(2) h(2) ...
i(1) j(1) k(1) l(1) i(2) j(2) k(2) l(2)
There is no rule for the number of input veectors, or number of the output rows, just vectors with the same number of elements, and an arrangement as above. The result than sould be the matrix created from the first elements of the vectors and than (concatonatd to it) the matrix cunstructed from the second elements, and so on. The result is a two dimensional array every time.
Stephen Cobeldick
Stephen Cobeldick on 11 Jan 2021
"There is no rule for the number of input veectors"
The product of the arrangement vector exactly specifies how many vectors you would have to have.
In the general case rather than having lots of vectors stored as separate variables (which would be very bad data design) it would be much better to store them in one numeric matrix or one cell array. An example with a cell array:
C = num2cell(reshape(1:12*3,3,12).',2); % {[1,2,3],[4,5,6],...,[34,35,36]}
A = [3,4]; % arrangement
D = reshape(C,A(2),A(1)).';
M = reshape(vertcat(D{:}),A(1),[])
M = 3×12
1 4 7 10 2 5 8 11 3 6 9 12 13 16 19 22 14 17 20 23 15 18 21 24 25 28 31 34 26 29 32 35 27 30 33 36
If the data were stored in one numeric matrix then you could just use permute and reshape.

Sign in to comment.

More Answers (1)

Mathieu NOE
Mathieu NOE on 11 Jan 2021
here you are
no loops
res = zeros(2,2*size(a,2));
res(1,1:2:2*size(a,2)) = a;
res(1,2:2:2*size(a,2)) = b;
res(2,1:2:2*size(a,2)) = c;
res(2,2:2:2*size(a,2)) = d;


Sign in to comment.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!