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

2 views (last 30 days)
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 Comment
John D'Errico
John D'Errico on 4 Dec 2021
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.

Sign in to comment.

Answers (2)

Matt J
Matt J on 4 Dec 2021
Edited: Matt J on 4 Dec 2021
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 Comment
Alejandro Zuniga
Alejandro Zuniga on 4 Dec 2021
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!

Sign in to comment.


Dave B
Dave B on 4 Dec 2021
Edited: Dave B on 4 Dec 2021
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 Comments
Dave B
Dave B on 4 Dec 2021
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 :)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by