Multiple answers into single array
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
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
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
Thanks for the reply,
When trying to generate a plot, I am hoping for every value of a (10.1, 10.2, 10.3,........, 14) I can plot the corresponding maximum value of M.
For example: when a=10.1, M=10.0987
That makes sense.
In that case, simply plot area_of_interest against the M you get by any of the methods I laid out above.
Thank you so much for your help, it is greatly appreciated
Clayton,
I do actually have another quick question if that is okay.
So the array for M is working and that is all good.
I was wondering whether I could also create another seperare array which will tell me the index of each of the maximums?
Therefore I will have a single array (M) with 41 maximum values and another single array which has the corresponding index of the 41 maximum values.
I hope this makes sense.
Cheers
Clayton Gotberg
2021년 4월 26일
편집: Clayton Gotberg
2021년 4월 26일
It totally makes sense. One great feature of the max function is that you can also ask it to provide the index of the maximum value. If you tell the max function to give two outputs, the second one will be the index. For example:
[max_value, index] = max(array);
I should mention that if the maximum value occurs multiple times, index will only be for the earliest time it appears. If you have more questions about exactly how the function works, I recommend reading the documentation page.
Okay awesome,
And then if I would like to put those index values into a single array by themselves, similar to what I did with the maximums, how would it be best to do this?
basically how would I get an array to look like:
index:
3 5 8 33 42
instead of
index
3
index
5
index
8
Sorry for the many questions, the help is incredibly appreciated
I'm happy to be of help!
You'd do it identically to how you made a matrix for the maximums. For now, I'll leave it at that - if you get stuck, let me know and I can explain indexing in more detail.
I actually think I have worked it out myself, I now have this in my workspace:
theta = linspace(0,2*pi);
M=[];
N=[];
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)];
[max_value, index] = max(bt);
N= [N index];
end
M
N
A=(10:0.1:14);
plot(A,M)
That should totally work,but I'd change a few things to help make it cleaner and faster.
theta = linspace(0,2*pi);
A=10:0.1:14; % No need for parentheses
M=zeros(size(A)); % Preallocation saves time
N=M;
count = 1;
for a = A % Now you don't generate A twice
c = 30-a;
bt = -a.*sin(theta)+((a.^2*sin(theta).*cos(theta))/sqrt(c.^2-a.^2*sin(theta).^2));
[M(count), N(count)] = max(bt); % No reason to calculate max again
end
M
N
plot(A,M)
I'd also recommend using more descriptive variable names (I'm on mobile or I'd be doing it too). Obviously you aren't going to forget what bt is tonight, but if you have to refer back to this in six months you'll be glad you don't have to basically resolve the question. It also improves readability for whoever looks at your code later.
Thanks so much Clayton
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개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
