How do I save vector values from for loop?

18 次查看(过去 30 天)
I have this section of code here
az = 0:90:270;
for k = 1:length(az)
rR = 0:.1:1;
L1 = (rR.*rR.*.5)+(mu.*rR.*thetanaught.*sind(az(k)))+(.5.*thetanaught.*mu.^2.*sind(az(k)).^2)+(.5.*twist.*rR.^3)+(mu.*twist.*rR.^2.*sind(az(k)))+(.5.*twist.*mu^2.*rR.*sind(az(k)).^2)+(.5.*rR.^2.*Theta_1c.*cosd(az(k)))+(rR.*mu.*Theta_1c.*sind(az(k)).*cosd(az(k)));
L2 = (.5.*rR.^2.*Theta_1s.*sind(az(k)))+(.5.*mu.^2.*Theta_1s.*sind(az(k)).^3)-(.5.*lambda.*rR)-(.5.*mu.*lambda.*sind(az(k)))-(.5.*rR.*mu.*Beta.*cosd(az(k)))-(.5.*mu.^2*Beta.*cosd(az(k)).*sind(az(k)));
Lc = L1+L2
end
I want to be able to plot Lc for each of the az values. So it looks something like this.
What is an efficient way to get matlab to store the vectors from each loop so I can do this? The way I was doing it before was just saving the values and I ended up with a 1x44 vector.

采纳的回答

Voss
Voss 2024-4-4,21:35
编辑:Voss 2024-4-4,21:36
az = 0:90:270;
rR = 0:0.1:1; % pull rR out of the loop (it never changes)
N = numel(az);
Lc = zeros(numel(rR),N); % make Lc a matrix with N columns
for k = 1:N
L1 = (rR.*rR.*0.5)+(mu.*rR.*thetanaught.*sind(az(k)))+(0.5.*thetanaught.*mu.^2.*sind(az(k)).^2)+(0.5.*twist.*rR.^3)+(mu.*twist.*rR.^2.*sind(az(k)))+(0.5.*twist.*mu^2.*rR.*sind(az(k)).^2)+(0.5.*rR.^2.*Theta_1c.*cosd(az(k)))+(rR.*mu.*Theta_1c.*sind(az(k)).*cosd(az(k)));
L2 = (0.5.*rR.^2.*Theta_1s.*sind(az(k)))+(0.5.*mu.^2.*Theta_1s.*sind(az(k)).^3)-(0.5.*lambda.*rR)-(0.5.*mu.*lambda.*sind(az(k)))-(0.5.*rR.*mu.*Beta.*cosd(az(k)))-(0.5.*mu.^2*Beta.*cosd(az(k)).*sind(az(k)));
Lc(:,k) = L1+L2; % store each result in a column of Lc
end
figure
plot(Lc) % plot N lines (each column of Lc is plotted as a separate line)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by