Multiple answers into single array

조회 수: 14 (최근 30일)
Brayden Kirk
Brayden Kirk 2021년 4월 26일
댓글: Clayton Gotberg 2021년 4월 26일
Below is my current workspace in MATLAB.
theta = linspace(0,2*pi);
for a = [10:0.1:14]
c = 30-a;
bt = -a.*sin(theta)+((a.^2*sin(theta).*cos(theta))/sqrt(c.^2-a.^2*sin(theta).^2));
M = [max(bt)]
end
When I run this file it gives me the maximum value of 41 arrays. The answer shows like this:
M =
13.5983
M =
13.6983
M =
13.7983
M =
13.8983
M =
13.9982
I am wondering is there a way I can make my answer into a single array which I can then plot against a(10:14)?
I wish for it to look something like this:
M =
13.5983 13.6983 13.7983 13.8983 13.9982
OR
M =
13.5983
13.6983
13.7983
13.8983
13.9982
Any help would be great

채택된 답변

Clayton Gotberg
Clayton Gotberg 2021년 4월 26일
편집: Clayton Gotberg 2021년 4월 26일
This is definitely possible! Since you aren't using integers for indexing, there are a couple of ways to do it:
theta = linspace(0,2*pi);
M = []; % Create empty M
for a = [10:0.1:14]
c = 30-a;
bt = -a.*sin(theta)+((a.^2*sin(theta).*cos(theta))/sqrt(c.^2-a.^2*sin(theta).^2));
M = [M max(bt)]; % Add max(bt) to the end of M every loop
end
% Also
theta = linspace(0,2*pi);
count = 1; % Create empty M
for a = [10:0.1:14]
c = 30-a;
bt = -a.*sin(theta)+((a.^2*sin(theta).*cos(theta))/sqrt(c.^2-a.^2*sin(theta).^2));
M(count) = max(bt);
count = count+1;
end
% Or
theta = linspace(0,2*pi);
area_of_interest = 10:0.1:14;
count = length(area_of_interest);
for k = 1:count
a = area_of_interest(k)
c = 30-a;
bt = -a.*sin(theta)+((a.^2*sin(theta).*cos(theta))/sqrt(c.^2-a.^2*sin(theta).^2));
M(k) = max(bt); % Add max(bt) to the end of M every loop
end
For plotting against a(10:14), I want to check about what you mean.
If you want to plot the maximum against a from 10 to 14, just use plot(a,M). If you want to plot only against the five values a = [10 ,11, 12 ,13, 14], you can just change the area of interest from 10:0.1:14 to 10:14. If you want to plot the 10th through 14th elements of a and M, you can say plot(a(10:14),M(10:14)).
  댓글 수: 11
Brayden Kirk
Brayden Kirk 2021년 4월 26일
Thanks so much Clayton
Clayton Gotberg
Clayton Gotberg 2021년 4월 26일
I'm glad I could help! If you feel that this question has been fully answered, please accept my answer in order to keep the number of open questions minimized. If you need anything else, though, just ask! I also recommend the MATLAB online training courses (such as the MATLAB Onramp) if you feel like a more general tutorial would be helpful.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by