- 1 x 2 x anything
- 2 x 1 x anything
- 2 x 2 x anything
assemble matrices for any matrix size with error of matrix dimension must agree
2 次查看(过去 30 天)
显示 更早的评论
Hi all,
I am trying to assmeble matrices, I already wrote a code for which I could do, but I would like to do edit/develop commands so it work for any matrix size.
Now my matrix is 2*2 if i change it to 4*4, or 5*5 and so on.. I get matrix dimension must agree ?
Looking for any help!
clearvars
clc;
NE=2;
NN=NE+1;
NDOF=NN*2
ke=[1 -1;-1 1]; % IF I change this matrix to be 4*4 or 5*5 and so on... will not work to assemble
ke=[1 -1 3;-1 1 5];
K=zeros(NDOF);
for i=1:NE
K(i:i+1,i:i+1)=K(i:i+1,i:i+1)+ke
end
0 个评论
采纳的回答
Walter Roberson
2021-1-31
编辑:Walter Roberson
2021-1-31
Why would it assemble? i:i+1 is a vector of length 2. You use it for both indices of K, so K(i:i+1,i:i+1) is going to talk about a 2 x 2 array. You take that 2 x 2 array extracted from K and you add all of ke to it. If ke is not a compatible size to the 2 x 2 array you extracted, then the addition is going to fail.
The compatible sizes of array that can be added to a 2 x 2 array are:
If you want to add all of ke to something extracted from K, then you need the extracted size to be compatible with the size of ke. For example,
NE=2;
NN=NE+1;
NDOF=NN*2
ke=[1 -1 3;-1 1 5];
[r, c, ~] = size(ke);
K=zeros(NDOF);
for i=1:NE
K(i:i+r-1,i:i+c-1)=K(i:i+r-1,i:i+c-1)+ke;
end
K
2 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!