I have a function with the following for-loop that seems to be pretty efficient. However, I'm wondering if this is the most efficient implementation, or is there a faster way to do this computation. There is a helper function too "Pnm_index" that finds the proper index to store the computations in the final output variable Pnms. I've included that function as well below. Since this helper function is so simple I'm also wondering if eliminating it will provide any speed up. Any insight is appreciated!
tic
s = sin(3*pi/7);
c = cos(3*pi/7);
p = 5; % input variable
Pnms = zeros(1,((p+1)*(p+2))*(2^-1)); % preallocate the output
Pn0 = 1.0; % initialize
fact = 1.0; % initialize
for m = 0:p
Pnm = Pn0;
i = Pnm_index(m, m); % find the proper index, see function below
Pnms(i) = Pnm; % store the output
tmp1 = Pnm;
Pnm = c * (2 * m + 1) * tmp1;
for n = m+1:p
i = Pnm_index(n, m); % find the proper index, see function below
Pnms(i) = Pnm; % store the output
tmp2 = tmp1;
tmp1 = Pnm;
Pnm = (c * (2 * n + 1) * tmp1 - (n + m) * tmp2) / (n - m + 1);
Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.
Translated by
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.