Find range between two array's indexes
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Max Bernstein
am 20 Jul. 2016
Kommentiert: Stephen23
am 20 Jul. 2016
Hello,
I have two arrays of different sizes and I Would like to find certain range between them, where the start index is from one array and ends with another array. For example:
A = [4192 23330 23342 29974 49089 49093 49096 55753 57035 57039]
B = [22780 48556 56522]
The desired output would be
C = [4192 22780; 29974 48556 ; 49096 56522]
Note that A and B can change in size, but array B is always the end point value.
Thanks, Max
1 Kommentar
Akzeptierte Antwort
Stephen23
am 20 Jul. 2016
Bearbeitet: Stephen23
am 20 Jul. 2016
Assuming that the rule really should pick the closest values from A that a re less than the values in B:
>> A = [4192 23330 23342 29974 49089 49093 49096 55753 57035 57039];
>> B = [22780 48556 56522];
>>
>> X = A(any(diff(bsxfun(@lt,A(:),B(:).')),2));
>> C = [X(:),B(:)]
C =
4192 22780
29974 48556
55753 56522
2 Kommentare
Stephen23
am 20 Jul. 2016
@Max Bernstein: this error happens because the vector B you are using is a column vector, whereas the vector B you gave in your example is a row vector. I fixed my code to work with both of these.
Weitere Antworten (1)
Thorsten
am 20 Jul. 2016
If you use that highest value that is still smaller than the corresponding B, you can write:
C = [A(arrayfun(@(i) find(A < B(i), 1, 'last'), 1:numel(B))); B]'
But this differs from your desired output. So what is the rule according to which you pick the values form A?
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!