Finding if a particular value is found in a range of arrays?
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
Stephen23
on 22 Jun 2015
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.
yashvin
on 22 Jun 2015
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
More Answers (0)
Categories
Find more on Whos 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!