MATLAB: Creating a new matrix based on the data from other two matrices

creating matrix with data from other two matrix

I have two matrices:
g1=[0.3 0.3 0.4 0.4 0.5 0.5 …;
0.1 0.2 0.1 0.2 0.1 0.2 …];
g2=[0.3 0.3 0.3 0.4 0.4 0.4 0.5 0.5 0.5 …;
0.3 0.4 0.5 0.3 0.4 0.5 0.3 0.4 0.5];
Matrix g1 is 2 x (2*nx) dimension, and matrix g2 is 2 x (3*nx) – where nx depends from one of the previous steps.
Now, I want to make a new matrix which will take first two rows of matrix g1, then first three rows of matrix g2, then again two rows of matrix g1, three rows of matrix g2 and so on up to the end.
So the final matrix should looks:
g=[0.3 0.3 0.3 0.3 0.3 0.4 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.5 …;
0.1 0.2 0.3 0.4 0.5 0.1 0.2 0.3 0.4 0.5 0.1 0.2 0.3 0.4 0.5 …];
clc;clear;close all;
nx=3; % for example (it should be calculated)
g_1=[0.3 0.3 0.4 0.4 0.5 0.5;
0.1 0.2 0.1 0.2 0.1 0.2];
g_2=[0.3 0.3 0.3 0.4 0.4 0.4 0.5 0.5 0.5;
0.3 0.4 0.5 0.3 0.4 0.5 0.3 0.4 0.5];
for i=1:nx
g=[g_1(:,1+(i-1)*2:i*2) g_2(:,1+(i-1)*3:i*3)]
i=i+1
end
g
In this way, I get only last five terms for A:
g =
0.5000 0.5000 0.5000 0.5000 0.5000
0.1000 0.2000 0.3000 0.4000 0.5000
I know in the definition of matrix g, it should be something like g(i), but I don't know how to do it. Any help would be appreciated.
Thanks in advance.

Best Answer

out = reshape([reshape(g1',2,[],2);reshape(g2',3,[],2)],[],2)'