필터 지우기
필터 지우기

Create matrix...please help me..

조회 수: 4 (최근 30일)
andy ganteng
andy ganteng 2011년 11월 2일
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.

채택된 답변

Sven
Sven 2011년 11월 2일
% 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
andy ganteng
andy ganteng 2011년 11월 2일
thanks, that answer really helpful

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Naz
Naz 2011년 11월 2일
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);
C=zeros(m,2*n);
k=1;
for x=2:2:2*n
C(:,x-1)=A(:,k);
C(:,x)=B(:,k);
k+1;
end
  댓글 수: 1
andy ganteng
andy ganteng 2011년 11월 2일
thanks, the results is the same as answer above

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by