How to position groups of matrices inside a bigger matrix

조회 수: 2 (최근 30일)
Feliciano Döring
Feliciano Döring 2018년 4월 30일
댓글: Feliciano Döring 2018년 5월 2일
I have a program in which I have some matrices of varying size that runs through a piece of code, for the current moment i only have two matrices. What i want to know is how would i place the matrices 'M' in a bigger matrix of zeros in a way that the values always stay in the diagonal and the rest of the matrices are zeros. So, for example if a have two matrices with size 4x4 the matrix would be a 8x8 matrix with [1:4,1:4] and [5:8,5:8] being values and the rest zeros. I want to do it in a way that i can do it for n-matrices. Here is the code i use.
for iter=1:2
D=Stations{iter};
n=length(D);
c=[0;0;0;0];
rowvector=ones(1,n);
dt0=rowvector
dn=sqrt((D(1,:)-m0(2)).^2+(D(2,:)-m0(3)).^2+(D(3,:)-m0(4)).^2);
dtx = -(D(1,:)-m0(2))./(v.*dn);
dty = -(D(2,:)-m0(3))./(v.*dn);
dtz = -(D(3,:)-m0(4))./(v.*dn);
M=[dt0; dtx; dty; dtz];
end
'Stations' is a cell array i use to store the different matrices
  댓글 수: 2
Bob Thompson
Bob Thompson 2018년 4월 30일
Does the code work? If not what kind of errors do you get?
Will your matrices always be square?
Feliciano Döring
Feliciano Döring 2018년 5월 1일
The code works fine, The matrices will have varying size and dont always have to be square

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

채택된 답변

Siyu Guo
Siyu Guo 2018년 5월 2일
blkdiag function is the tool you are looking for. Help it in MATLAB. Note you can use a loop within which the function is called iteratively to cope with n sub-matrices.

추가 답변 (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