How to create a for loop for this?

조회 수: 1 (최근 30일)
Michaela Byrne
Michaela Byrne 2022년 4월 5일
댓글: Voss 2022년 4월 5일
I haven't worked with for loops in a while and can't remember how to set up the for loop for what I'm trying to do. I have an vector, "w", of values and I want to find the difference between values. How would I set up a for loop to do what I did in the last section in my code where I find dw1 through dw3?

채택된 답변

Voss
Voss 2022년 4월 5일
편집: Voss 2022년 4월 5일
No loop is necessary; you can use diff (see below).
n = 4;
startFrequency = 0.25;
endFrequency = 1;
frequencies = linspace(startFrequency,endFrequency,n);
w = 2*pi*frequencies;
% no loop necessary:
dw = diff(w)
dw = 1×3
1.5708 1.5708 1.5708
% if you want to use a loop anyway, here's one:
for ii = 1:n-1
dw(ii) = w(ii+1)-w(ii);
end
dw
dw = 1×3
1.5708 1.5708 1.5708
  댓글 수: 2
Michaela Byrne
Michaela Byrne 2022년 4월 5일
Thank you!
Voss
Voss 2022년 4월 5일
You're welcome!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by