How do I save vector values from for loop?
1 次查看(过去 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.
0 个评论
采纳的回答
Voss
2024-4-4
编辑:Voss
2024-4-4
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!