Operations on portions of matrices to the end of the matrices.

First, let me start off by thanking this matlab answers community for its tremendous support for those of us new to the mathematical programming world. I've already been helped immensely by many great minds.
I'm running across an issue at the moment that I cannot wrap my head around and could use some help. I have two matrices, K & Beta. K is always 'n' rows by 4 columns. Beta is also always 'n' rows but can possibly have more columns than K.
The problem is to do mathematical operations on portions of the K and Beta matrices and combine the results in a K_structure matrix. Essentially I want to do the following:
K_structure = Beta(1:4,:)'*K(1:4,:)*Beta(1:4,:) + Beta(5:8,:)'*K(5:8,:)*Beta(5:8,:) + Beta(9:12,:)'*K(9:12,:)*Beta(9:12,:) + ...
So, the first 4 rows of Beta' times the first 4 rows of K times the first 4 rows of Beta then plus the next 4 rows of Beta' times the next 4 rows of K times the next 4 rows of Beta + ... and so on until the end of Beta and K are reached. Based on the program the end row is a multiple of 4 (i.e. 4, 8, 12, 16, etc.)
Any help would be terrific.

 채택된 답변

Matt J
Matt J 2013년 3월 11일
편집: Matt J 2013년 3월 11일
Another approach, using FEX:mat2tiles
Kd=mat2tiles(sparse(K),[4,4]);
Kd=blkdiag(Kd{:});
K_structure = Beta.'*Kd*Beta;
Essentially, you should really be maintaining K as a (sparse) block diagonal matrix with 4x4 blocks instead of stacking the 4x4 blocks in an 4mx4 matrix.

댓글 수: 2

I think I need to buy you a drink if I ever meet you Matt.
I'm getting an error with the 'mat2tiles' function. Perhaps it's in a library I don't have or not included with the student version? I see on the link you sent that it only mentions 2012b I have student 2012a grrr.
Undefined function 'mat2tiles' for input arguments of type 'double'.
Error in Truss_2D (line 68) Kd=mat2tiles(sparse(K),[4,4]);
I figured out it was a function needed that you so kindly provided. Thanks again. I'll have to see if I can spend some time trying to understand how the function works. Thanks

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

추가 답변 (1개)

Honglei Chen
Honglei Chen 2013년 3월 11일
편집: Honglei Chen 2013년 3월 12일
Why not just use a for loop?
Let's say your K is 12 rows, so
Niteration = 12/4;
K_structure = zeros(size(Beta,2));
for m = 1:Niteration
idx = (1:4)+(m-1)*4;
K_structure = K_structure + Beta(idx,:)'*K(idx,:)*Beta(idx,:);
end

댓글 수: 3

I modified it a little to be:
Niteration = 12/4; K_structure = zeros(size(Beta,2)) for m = 1:Niteration idx5 = 1:4+(m-1)*4; K_structure = K_structure + Beta(idx5,:)'*K(idx5,:)*Beta(idx5,:); end K_structure
and I got an error:
Error using * Inner matrix dimensions must agree.
Error in Truss_2D (line 71) K_structure = K_structure + Beta(idx5,:)'*K(idx5,:)*Beta(idx5,:);
To be honest, I'm not sure why I'm getting this error. it should be the transpose of a 4 row by 6 column matrix times a 4 row by 4 column matrix times a 4 row by 6 column matrix and each of those results added together. I must be missing something.
if I run the program with just
K_structure = Beta(1:4,:)'*K(1:4,:)*Beta(1:4,:)+Beta(5:8,:)'*K(5:8,:)*Beta(5:8,:)+Beta(9:12,:)'*K(9:12,:)*Beta(9:12,:)
I get the following results:
1.0000 0 -1.0000 0 0 0
0 1.0000 0 0 0 -1.0000
-1.0000 0 1.3600 -0.4800 -0.3600 0.4800
0 0 -0.4800 0.6400 0.4800 -0.6400
0 0 -0.3600 0.4800 0.3600 -0.4800
0 -1.0000 0.4800 -0.6400 -0.4800 1.6400
I forgot a bracket in my answer. I updated it.

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

질문:

2013년 3월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by