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!