フィルターのクリア

Replace number if within 2 numbers and smaller than last number.

1 回表示 (過去 30 日間)
Jonathan Cheong
Jonathan Cheong 2021 年 3 月 9 日
コメント済み: Walter Roberson 2021 年 3 月 9 日
Hello, I have a 45x2 array of numbers called 'ddindex'. The 1st row consist of starting number. 2nd row is the ending number.
Then, I have a list of numbers called 'indend2'. If indend2 is within these 2 numbers and is smaller than the ending number, replace indend2 with the ending number.
For example:
A = [1 15] B = [8, 42, 100...]
[20 36]
[37 50]
The output would be:
Output = [1 8]
[20 36]
[37 42]
As can be seen the 2nd row doesn't change because the number doesn't fall within the range of these values.
15 is replaced by 8, because 8 is smaller, and 50 is replaced by 42, because 42 is smaller and both numbers within these values.
How can I achieve this? Many thanks.

採用された回答

Walter Roberson
Walter Roberson 2021 年 3 月 9 日
A = [1 15; 20 36; 37 50]
A = 3×2
1 15 20 36 37 50
B = [8, 42, 100]
B = 1×3
8 42 100
Select = @(M,idx) M(idx)
Select = function_handle with value:
@(M,idx)M(idx)
PW = @(cond,val1,val2) Select([val1,val2], [cond, ~cond])
PW = function_handle with value:
@(cond,val1,val2)Select([val1,val2],[cond,~cond])
cell2mat(arrayfun(@(row,b) [A(row,1), PW( b>=A(row,1)&b<=A(row,2), b, A(row,2)) ], (1:size(A,1)).', B(:), 'uniform', 0))
ans = 3×2
1 8 20 36 37 50
  2 件のコメント
Jonathan Cheong
Jonathan Cheong 2021 年 3 月 9 日
Thanks again Walter!
Walter Roberson
Walter Roberson 2021 年 3 月 9 日
A = [1 15; 20 36; 37 50]
A = 3×2
1 15 20 36 37 50
B = [8, 42, 100]
B = 1×3
8 42 100
cell2mat(arrayfun(@(row,b) [A(row,1), min(A(row,2), b./(b>=A(row,1)&b<=A(row,2)))], (1:size(A,1)).', B(:), 'uniform', 0))
ans = 3×2
1 8 20 36 37 50

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by