code explanation question help!
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
%can you please explain what this code is calcuating?
%Thanks!
y= x(find(t > t(i)-0.3))
채택된 답변
0 개 추천
'i' is an integer, index value. For the sake of the example, let's say i = 4.
t(i) is the 4th value stored in 't'.
t > t(i)-0.3 produces a logical vector of 1s and 0s the same size as 't'. It contains 1s whenever the value of t is greater than the 4th t plus 0.03.
So, if t = [0 1 2 3 4 5 6 7] and i = 4, the logical vector would be [0 0 0 1 1 1 1 1].
find() returns the index values that are true. so, find([0 0 0 1 1 1 1 1]) would return [4,5,6,7,8].
x([4,5,6,7,8]) looks at the fourth, fifith, sixth, seventh, and eighth values of 'x'.
To summarize, that line pulls out a subsection of 'x' based on the values of 't'.
댓글 수: 9
Thank you Adam!
still little confused, how would this then apply to the following:
for i=1:length(t)
if t(i)<0.3
y=x(1:i);
else
y= x(find(t > t(i)-0.3));
end
Walter Roberson
2019년 4월 1일
... "the 4th t" minus 0.03
william Smith
2019년 4월 1일
shouldnt be minus 0.3
william Smith:
Each iteration of your for loop writes over all of y. The end result is going to be the same as if you had only done the last iteration, when i=length(t) .
Note:
x(find(t>t(i)-0.3))
can be done more efficiently by
x(t>t(i)-0.3)
length(t) just tells you how 'long' t is. For example, lenght([4 5 6 7 8]) = 5 since there are 5 values in that vector.
for i = 1:length(t) this creates a for-loop. The code within the for-loop will use the value 1 on the first iteration and then the value 2, and so on. It will stop when it creates the length of t.
So, on the 4th 'iteration', t(i) will access the 4th value in t.
Here's a walk-through example
t = [4 5 6 7 8 9 10 11 12]';
length(t) % = 9
for i = 1:length(t) % for values 1 to 9
if t(i)<0.3 % on 1st iteration, t(1) = 4; 4 < 0.3 is false
y=x(1:i); % this line is executed only when t(i) < 0.3
else % the line below is executed all other times
y= x(find(t > t(i)-0.3)); %see my previous explanation
end
end
william Smith
2019년 4월 1일
Thank you Walter,
my confusion comes from 0.3 in the last line, if i try to integrate y over 0.3 seconds,
so what i am trying to do is if t<0.3 then i use total time until i reach 0.3 then integrate as normal for values above 0.3.
would this even work?
Thanks again!
william Smith
2019년 4월 1일
Pefrect Thank you Adam and Walter!!
I have a feeling this isn't the full section of code. Now that there's context, I might be able to help you further. Where are you doing the integration? Could you describe x?
william Smith
2019년 4월 1일
finding the force that needed to be applied on a mass where i have a broom attached to that mass and swings around (PID design controller problem)
F(i) = k_p*x(i) + k_i*trapz(y), as far as x it represents the angle that the broom swings at.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 MATLAB Coder에 대해 자세히 알아보기
태그
참고 항목
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)
