How do I find the lowest values in one array that are greater than each value from another array?

조회 수: 14 (최근 30일)
I've got two arrays, for example:
x=[5 16 27]';
y=[1 3 7 8 9 10 12 13 15 17 21 24 28 31 54]';
I want to create a variable z that returns the lowest values in y that are greater than each of the values in x. In this example z should return 7, 17, and 28.
I've tried a comnbination of min and > but can't make it work.
Thanks!

답변 (1개)

Naman Bhaia
Naman Bhaia 2019년 5월 3일
Hello Liam,
Can you try if the following code helps with the problem you have?
z=zeros(1,0); %defining an array z to store output in
for i=1:3 %depends on size of array x
t=y(y>x(i)); %this will store all elements in t that are greater than x(i)
z(end+1)=t(1); %this will extract the first element of t which is also the smallest (since y was in ascending order)
end

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by