필터 지우기
필터 지우기

Using for loops to check elements

조회 수: 1 (최근 30일)
Vishan Gupta
Vishan Gupta 2018년 9월 28일
댓글: Guillaume 2018년 9월 28일
I have vector a=[1 2 4 4 2 1] and I want to create a for loop that does some calculations between each element of a, i.e. it performs the calculations between 1 and 2, then between 2 and 4, then between 4 and 4, 4 and 2, then 2 and 1. How can I do this? Thanks.
  댓글 수: 1
Guillaume
Guillaume 2018년 9월 28일
It might be entirely possible that the loop is not needed. Details of the calculations would be required to know for sure.
E.g if you just wanted to do 3*a(i)-2*a(i+1)^2, this would simply be:
result = 3*a(1:end-1) - 2*a(2:end).^2

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

채택된 답변

Bruno Luong
Bruno Luong 2018년 9월 28일
for i=1:length(a)-1
interval = a([i i+1]);
dosomething(interval);
end

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by