How to assemble coefficient matrix?

6 Ansichten (letzte 30 Tage)
Christopher
Christopher am 12 Jun. 2013
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 Kommentar
Christopher
Christopher am 12 Jun. 2013
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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Roger Stafford
Roger Stafford am 12 Jun. 2013
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 Kommentare
Christopher
Christopher am 12 Jun. 2013
Thank you Roger
One question though, what purpose does the IC=zeros(N) line serve?
Thanks again
Roger Stafford
Roger Stafford am 12 Jun. 2013
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by