How can I insert a bunch of 6x6 matrices into a larger 30x30 matrix?
14 次查看(过去 30 天)
显示 更早的评论
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
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 个)
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
2 个评论
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}
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 :)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!