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

조회 수: 1 (최근 30일)
FEM_Analysis_1000
FEM_Analysis_1000 2019년 12월 23일
답변: Andrei Bobrov 2019년 12월 23일
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