How can I insert a bunch of 6x6 matrices into a larger 30x30 matrix?

조회 수: 14 (최근 30일)
Alejandro Zuniga
Alejandro Zuniga 2021년 12월 4일
댓글: Dave B 2021년 12월 4일
I have a bunch of 6x6 matrices that I need to insert into a larger 30x30 zeros matrix. I already have all of my 6x6 matrices defined as x1,x2,x3,..etc., but I need to be able to insert these smaller matrices into certain columns and rows of the larger 30x30 matrix.
I was able to find this other post:
but I was still confused on how to proceed with larger matrices.
I'm just trying to figure out how to do this for ONE matrix right now. I'd eventually like to be able to make a loop as some of these values will overlap, but I'll save that for later. I figured I'd rather end up with a bunch of 30x30 matrices and then just add them all up.
  댓글 수: 1
John D'Errico
John D'Errico 2021년 12월 4일
This is why you don't want to define many numbered variables. Learn to use matrices. In this case, a 3 dimensional 6x6xn matrix might have been a good idea. But now that you have all of those named and numbered matrices, you are stuck.

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

답변 (2개)

Matt J
Matt J 2021년 12월 4일
편집: Matt J 2021년 12월 4일
Example:
A=zeros(30);
A(1:6,1:6)=reshape(1:36,6,6)
A = 30×30
1 7 13 19 25 31 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 8 14 20 26 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 9 15 21 27 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 10 16 22 28 34 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 11 17 23 29 35 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 12 18 24 30 36 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  댓글 수: 1
Alejandro Zuniga
Alejandro Zuniga 2021년 12월 4일
Would the reshape function work to split the 6x6 matrix into smaller 3x3 matrices which I can then insert into selected locations for my 30x30 matrix? So that it would look like this:
X =
1 1 1 0 0 0 1 1 1 0
1 1 1 0 0 0 1 1 1 0
1 1 1 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 1 1 1 0
1 1 1 0 0 0 1 1 1 0
1 1 1 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Thank you for taking your time to reply to my question!

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


Dave B
Dave B 2021년 12월 4일
편집: Dave B 2021년 12월 4일
I think you're saying: you have an existing 30x30 matrix and some existing 6x6 matrices, and you want to put the values from one of the 6x6 matrices into the 30x30 at some known location...replacing the values that are already in there?
X=zeros(30);
x1=ones(6);
x2=ones(6)*2;
x3=ones(6)*3;
X(1:6,1:6) =x1;
X(1:6,7:12)=x2;
X(7:12,1:6)=x3;
X
X = 30×30
1 1 1 1 1 1 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  댓글 수: 2
Alejandro Zuniga
Alejandro Zuniga 2021년 12월 4일
yes!, but i think I messed up the wording of my question. Some of my 6x6 matrices would need to be split up into 3x3 matrices and then insert those 3x3 into selected parts of the larger 30x30 matrix. So in my case i have a ones(6,6) matrix that i would add into a 30x30 zeros matrix and it would look somethin like this:
X =
1 1 1 0 0 0 1 1 1 0
1 1 1 0 0 0 1 1 1 0
1 1 1 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 1 1 1 0
1 1 1 0 0 0 1 1 1 0
1 1 1 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
This would be the top left corner of the 30x30 zeros matrix, but I would have to do this separation for multiple 6x6 matrices. I don't mind doing some grunt work and just copying and pasting the snippet of code to change the position of where each 6x6 matrix will be, but I don't have the expertise to write it as a loop.
From here I'd would just label/define each 30x30 matrix and just add them all up to get my final 30x30 matrix.
I don't even know if this is possible or if I would need to start all over and figure out some other way to solve it.
I haven't seen anyone else be able to do this so who knows? Thank you for taking the time to reply to my question!
Dave B
Dave B 2021년 12월 4일
I think what's missing is the rule, i.e. what defines how the matrices get split up and where they go. It's entirely possible to do this in a loop, but somewhere you're going to have to define the logic of where these should go.
For instance, maybe you're just going to specify some upper left corners:
x1=ones(3);
x2=ones(3)*2;
x3=ones(3)*3;
x4=ones(3)*4;
rows = [1 1 6 6];
cols = [1 7 1 7];
xs={x1 x2 x3 x4}
xs = 1×4 cell array
{3×3 double} {3×3 double} {3×3 double} {3×3 double}
X=zeros(10);
for i = 1:4
[h,w]=size(xs{i});
X(rows(i):rows(i)+h-1,cols(i):cols(i)+w-1)=xs{i};
end
X
X = 10×10
1 1 1 0 0 0 2 2 2 0 1 1 1 0 0 0 2 2 2 0 1 1 1 0 0 0 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 0 0 0 4 4 4 0 3 3 3 0 0 0 4 4 4 0 3 3 3 0 0 0 4 4 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
But it sounds to me like you have something else in mind (split each matrix into 4 parts, place those parts with some amount of space in between, etc.) If you can describe the logic algorithmically, MATLAB almost certainly can do it prety easily :)

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

카테고리

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