How can I insert a bunch of 6x6 matrices into a larger 30x30 matrix?
14 views (last 30 days)
Show older comments
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
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.
Answers (2)
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
2 Comments
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}
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
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 :)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!