필터 지우기
필터 지우기

Find range between two array's indexes

조회 수: 2 (최근 30일)
Max Bernstein
Max Bernstein 2016년 7월 20일
댓글: Stephen23 2016년 7월 20일
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
Stephen23
Stephen23 2016년 7월 20일
편집: Stephen23 2016년 7월 20일
Should the example really be this? :
C = [4192 22780; 29974 48556 ; 55753 56522]
Or if not, please explain why 49096 is selected, and not 55753.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2016년 7월 20일
편집: Stephen23 2016년 7월 20일
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
Max Bernstein
Max Bernstein 2016년 7월 20일
Sorry for the mistake, but yes it should be the closest values from A that are less than B.
I got an error using your code:
>> X = A(any(diff(bsxfun(@lt,A(:),B)),2));
Error using bsxfun
Non-singleton dimensions of the two input arrays must match each other.
Stephen23
Stephen23 2016년 7월 20일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Thorsten
Thorsten 2016년 7월 20일
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?
  댓글 수: 1
Max Bernstein
Max Bernstein 2016년 7월 20일
Sorry for the mistake, but yes it should be the closest values from A that are less than B.
Your answer works great, my desired output is:
C = [A(arrayfun(@(i) find(A < B(i), 1, 'last'), 1:numel(B))), B]'
replaced ; with ,

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by