I have two matrices
a = randi ([0 10], 5, 6)
b = randi ([0 10], 2, 45)
Now I want to construct a matrix c of size 8 x 15 with all the elements of a and b. Is it possible to do it in a single line code? Some suggestions please.
Here is an example of what I’m trying to do:
a = [1 4 6;
5 8 0;
3 7 9;
4 10 5];
b = [5 6;
5 0];
c = [1 4 6 5;
8 0 3 7;
9 4 10 5;
5 6 5 0]
The specifications for how to combine
aandbaren’t clear. Here is one way to do it.Create a single column vector built from
aandb. Thenreshapethat column vector into a matrix.This will only work if the
numel(a) + numel(b)equals the total number of elements inc.Attempts to execute
c = reshape( [ a(:); b(:) ], 7,12);will fail as you aren’t providing enough elements to create an7x12matrix.Update
Noufal’s comment on this answer changes the problem reqs a bit. Basically you stil create the column vector but you only populate C depending on how many elements you have at your disposal: