Replace number if within 2 numbers and smaller than last number.
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Walter Roberson
2021-3-9
A = [1 15; 20 36; 37 50]
B = [8, 42, 100]
Select = @(M,idx) M(idx)
PW = @(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))
2 个评论
Walter Roberson
2021-3-9
A = [1 15; 20 36; 37 50]
B = [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))
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!