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

1 回表示 (過去 30 日間)
yashvin
yashvin 2015 年 6 月 22 日
コメント済み: yashvin 2015 年 6 月 28 日
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 件のコメント
yashvin
yashvin 2015 年 6 月 22 日
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.
Purushottama Rao
Purushottama Rao 2015 年 6 月 22 日
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.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2015 年 6 月 22 日
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 件のコメント
Walter Roberson
Walter Roberson 2015 年 6 月 24 日
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);
yashvin
yashvin 2015 年 6 月 28 日
Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by