Finding if a particular value is found in a range of arrays?

Hi My range of arrays is :
array_fl =
19950 20000 20050
20950 21000 21050
21950 22000 22050
22950 23000 23050
23950 24000 24050
24950 25000 25050
25950 26000 26050
26950 27000 27050
27950 28000 28050
28950 29000 29050
29950 30000 30050
30950 31000 31050
31950 32000 32050
32950 33000 33050
33950 34000 34050
34950 35000 35050
35950 36000 36050
36950 37000 37050
37950 38000 38050
38950 39000 39050
I want to know if a number is found in this particular range, if yes, pick up its middle value else take the number as it is.
find below the code i wrote
num_fl=20;
array_fl=[];
initial_range=19950:50:20050;
% generate a range of flight level
for i=1:num_fl
array_fl(i,:)=initial_range;
initial_range=initial_range+1000;
end
%%getting the correct altitude
FL=[];
%example
Alt_greater_than_20000=[20960 27030 22800 29000]
length_array=length(Alt_greater_than_20000);
% for i=1:length_array
for j=1:num_fl
for i=1:length_array
if Alt_greater_than_20000(i) >= array_fl(j,1) & Alt_greater_than_20000(i) <= array_fl(j,3)
FL(i)= array_fl(j,2);
else
FL(i)= Alt_greater_than_20000(i);
end
end
end
end
find also my code. Can i know where i went wrong?

3 Comments

Rather than giving us broken code, it would be much easier if you actually clearly explained exactly what you are trying to achieve, and give example data of both input and output values that we can try out, and explain why those are the output values.
We know MATLAB quite well, so we can figure out some code to help you if you explain what it is that you are trying to achieve.
For example, given that i have an array of numbers
Alt_greater_than_20000=[20960 27030 22800 29000]
For each number in the array, i want to know in which particular row of array_fl it lies. If this, i want to pick its middle value of the same row and store it. If it does not lie, i simply want to store it and continue with the next number.
For example for number 20960, i expect 21000, as it is located in the 2nd row of array_fl which is (20950 21000 21050).
for 27030, save it as 27000 (as it lies in range 26950 27000 27050)
for 22800, save it as 22800 (as it does not lie in any range)
for 29000, save it as 29000 (it lies in the range)
I expect the corresponding numbers to be [21000 27000 22800 29000]
I have also attached the M file of how i tried to do it.
The problem in your code is associated with the no of comparisons made. Say for example first element 20960 is compared with 20 rows of the array_fl. Conditon is met in the second row and you are changing the Fl element corresponding to it. But during the next iteration, 20960 is compared against 3rd row and the condition is not met, therefore you are reverting again FL element to 20960. That is why you had a wrong result.

Sign in to comment.

 Accepted Answer

length_array=length(Alt_greater_than_20000);
for K = 1 : length_array
fl_in_range = array_fl(:,1) <= Alt_greater_than_20000(K) & Array_greater_than_20000(K) <= array_fl(:,3);
if any(fl_in_range)
FL(K) = array_fl(fl_in_range,2);
else
FL(K) = Array_greater_than_20000(K);
end
end

8 Comments

What is Array_greater_than_20000? I am getting undefined...
Changing it to Alt_greater_than_20000..let me try
Yes it works!!! Thanks! The main thing was about "any". Thanks a lot!!!
There is actually a latent bug in Walter's code because of the use of any. If for some reason there happens to be more than one row of array_fn that matches, then any will return true and the following line, the assignment to Fl(K) will fail as it will try to assign an array to a scalar.
A safer approach would be to use sum. If it's 0, no row matches. If it's 1, exactly one row matches, if it's more than one, more than one row match:
switch sum(fl_in_range)
case 0 %no match
Fl(k) = Alt_greater_than_20000(K);
case 1 %exactly one match
Fl(k) = array_fn(fl_in_range, 2);
case 2 %more than one match
warning('more than one match found, using first one')
Fl(k) = array_fn(find(fl_in_range, 1), 2);
end
Alternatively, add code to check that the ranges in fl_in_range do not overlap.
I have one more question. If i want to eliminate the table ie the array_fl as the pattern will always be
19950 20000 20050
20950 21000 21050
...
How can i do it? It is just increment of 1000 between the rows. And if the value satisfies the range, it is only (19950+20050)/2 that i need to assign
Anyone there? Or should i create a new post for this clarification?
m = mod(Alt_greater_than_20000, 1000);
need_round = m <= 50 | m >= 950;
Alt_greater_than_20000(need_round) = 1000 * round(Alt_greater_than_20000(need_round)/1000);

Sign in to comment.

More Answers (0)

Asked:

on 22 Jun 2015

Commented:

on 28 Jun 2015

Community Treasure Hunt

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

Start Hunting!