Replace number if within 2 numbers and smaller than last number.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jonathan Cheong
am 9 Mär. 2021
Kommentiert: Walter Roberson
am 9 Mär. 2021
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 9 Mär. 2021
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 Kommentare
Walter Roberson
am 9 Mär. 2021
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))
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Dialog Boxes finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!