MATLAB Answers

andy ganteng

Create matrix...please help me..

Asked by andy ganteng
on 2 Nov 2011
Accepted Answer by Sven

I have 2 matrix, A(m,n) and B(o,p). which m=o and n=p, then n and p depend on user input. if input = 3 then n = p =3, if input =10 then n = p =10, and so on. Now, i wanna create matrix C which consist of A and B with the structure like this C=[A(:,1) B(:,1) A(:,2) B(:,2) ...A(:,n) B(:,p)] how can i create that matrix, with n and p depend on user input?iam confuse...please help.

thanks in advance.



No products are associated with this question.

2 Answers

Answer by Sven
on 2 Nov 2011
 Accepted answer

% So you have 2 matrices that are the same size.
A = rand(4,5)
B = rand(4,5)+50
% Stack them on top of each other
C = cat(1, A, B)
% Rearrange them to have the same 1st dim size as the original
C = reshape(C, size(A,1),[])

You see here that the "C" variable has columns like you requested C=[A(:,1) B(:,1) A(:,2) B(:,2) ...A(:,n) B(:,p)]

  1 Comment

thanks, that answer really helpful

Answer by Naz
on 2 Nov 2011

I am also confused. Let's say that the user gives you two matrices A and B. It seems like you need to insert columns of one matrix between columns of another (lust like mixing the playing cards). Since both matrices will be the same size we can do the following:

[m n]=size(A);
for x=2:2:2*n

  1 Comment

thanks, the results is the same as answer above

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!