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

1 次查看(过去 30 天)
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.

采纳的回答

Andrei Bobrov
Andrei Bobrov 2019-12-23
out = reshape([reshape(g1',2,[],2);reshape(g2',3,[],2)],[],2)'

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by