Extract the bigger number of values from the previous one in an array

조회 수: 1 (최근 30일)
Ali Najem
Ali Najem 2022년 9월 28일
댓글: Ali Najem 2022년 9월 28일
Dear experts,
I want to do this kind of loop
Data = rand(1,15);
for i=1:15
if Data(i) < Data(i+1)
X =10;
else
X=0;
end
end
X;
I suppose to get 15 values of X between 10 and 0 but I obtained this error
Index exceeds the number of array elements. Index must not exceed 15.
how can i proceed this loop into
  댓글 수: 1
Matt J
Matt J 2022년 9월 28일
I suppose to get 15 values of X between 10 and 0 but I obtained this error
There can't be 15 values, because when i=15, Data(i+1) doesn't exist.

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

채택된 답변

Matt J
Matt J 2022년 9월 28일
편집: Matt J 2022년 9월 28일
Easier to do,
Data = rand(1,15)
Data = 1×15
0.5961 0.1403 0.0904 0.4693 0.6848 0.1834 0.0516 0.4032 0.2979 0.5497 0.9613 0.9228 0.9208 0.8610 0.7979
X=10*(diff(Data)>0)
X = 1×14
0 0 10 10 0 0 10 0 10 10 0 0 0 0

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by