How to assemble coefficient matrix?

3 vues (au cours des 30 derniers jours)
Christopher
Christopher le 12 Juin 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 commentaire
Christopher
Christopher le 12 Juin 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

Connectez-vous pour commenter.

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by