Check if all previous array values meet a condition

조회 수: 2 (최근 30일)
Ikenna Okoye
Ikenna Okoye 2020년 5월 18일
댓글: Walter Roberson 2020년 5월 19일
Hi all, I have a problem where im trying to access all previously genereated values in a for loop. These values are all being stored in an array. How would I then check if the current object is less then (or any condition) all of the previous array values. If anyone has an idea of how to do this, it would be of great help, thanks.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 5월 18일
편집: Walter Roberson 2020년 5월 18일
%instantaneous, but values could be in any order
tf = A(idx) < min(A(1:idx-1))
%is it true that every value is less than the previous?
tf = all(diff(A(1:idx)) < 0)
%is it true that every value up to some point is less than some constant
tf_vector = cumprod(A < constant);
first_not = sum(tv_vector) + 1;
  댓글 수: 2
Ikenna Okoye
Ikenna Okoye 2020년 5월 19일
Hi Walter, just a few questions. What does 'idx' represent and is 'A' equal to the array that wlll be iterated through to check the coditions? It aslo seems that he three steps you have outlined would be how you are going through the problem? For my problem, i'm not sure if step 2 would be necessary, though i could be wrong.
Walter Roberson
Walter Roberson 2020년 5월 19일
idx is the index of the "current object" -- the location to be compared to previous objects.
A is the array being checked against.
The three sections there are in response to three different possible needs.
The first section is for the case where you do not care what order the previous entries are in and just need to know that the current entry is less than any of what went before.
The second section is for the case where values need to be strictly decreasing and you want to verify that for some particular location.
The third section is for the case where what you want to know is the first location that is not less than a particular value. It is equivalent to
first_not = length(A)+1;
for idx = 1 : length(A)
if ~(A(idx) < constant)
first_not = idx;
break;
end
end

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

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by