How to assemble coefficient matrix?

조회 수: 5 (최근 30일)
Christopher
Christopher 2013년 6월 12일
I am trying to assemble a N x N matrix for IC as shown above. I have vectors for ao(theta), c(theta), and thetal. I am trying to figure out how to assemble the IC matrix from these vectors using the equation above. From there I need to solve for the A vector values.
  댓글 수: 1
Christopher
Christopher 2013년 6월 12일
I believe the repmat command can be used to assemble this matrix, but I cannot figure out how I would write this out to generate the n x n IC matrix.
Thanks

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

채택된 답변

Roger Stafford
Roger Stafford 2013년 6월 12일
I'll assume you have a0, c, and theta as column vectors of corresponding elements and of length N.
IC = bsxfun(@plus,4*b./(a0.*c),(1./sin(theta))*(1:N)).*sin(theta*(1:N));
Of course it is much easier to write nested for loops:
IC = zeros(N);
for l = 1:N
for m = 1:N
IC(l,m) = (4*b/a(l)/c(l)+m/sin(theta(l)))*sin(m*theta(l));
end
end
Note: You ought to use something other than lowercase l for your index. It is too easily confused with the numeral 1.
  댓글 수: 2
Christopher
Christopher 2013년 6월 12일
Thank you Roger
One question though, what purpose does the IC=zeros(N) line serve?
Thanks again
Roger Stafford
Roger Stafford 2013년 6월 12일
Doing the initial 'zeros' preallocates memory for the IC array all at once rather than increasing it piecemeal in the for-loops. For large arrays the latter can slow down the execution of code greatly. This does not apply to vectorized code such as in the first method above using 'bsxfun' since these matlab functions do their own preallocation.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by