Finding if a particular value is found in a range of arrays?
1 view (last 30 days)
Show older comments
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
Purushottama Rao
on 22 Jun 2015
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.
Accepted Answer
Walter Roberson
on 22 Jun 2015
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
Walter Roberson
on 24 Jun 2015
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);
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!