필터 지우기
필터 지우기

Find adjacent values of a vector corresponding to another vector

조회 수: 4 (최근 30일)
anton fernando
anton fernando 2017년 12월 21일
답변: Jos (10584) 2017년 12월 22일
Hi, I am trying to find the adjacent points of a 1-D vector corresponding to the elements of another column vector. It is very hard to explain. I will explain it from an example.
A=[ 11; 22; 32; 44; 51;]; B=[11.4 32.4 36.6]; So I need a matrix with the adjacent points of each element of the vector B of vector A as in, Ans1=[11 22; 32 44; 32 44;] also the corresponding locations as in, ans2[1 2; 3 4; 3 4;] Appreciate any help.

채택된 답변

Matt J
Matt J 2017년 12월 21일
idx=discretize(B,A);
Ans2=[idx(:),idx(:)+1];
Ans1=A(Ans2);
  댓글 수: 7
anton fernando
anton fernando 2017년 12월 22일
편집: anton fernando 2017년 12월 22일
I am sorry. I meant by not sorted that there can be NaN values and repeated values in the vector. But initially I don't have to worry about it. But sure, I can flip the matrix and make it monotonically increasing. But vector A is monotonically decreasing.
anton fernando
anton fernando 2017년 12월 22일
Once you flipped the matrices it works. Thanks. :)

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

추가 답변 (1개)

Jos (10584)
Jos (10584) 2017년 12월 22일
Here is a solution that finds the element in A that is just before or just after each element in B.
A=[ 11; 22; 32; 44; 51;]; B=[11.4 32.4 36.6]
clear ans2
ans2(:,1) = nearestpoint(B, A, 'before') ;
ans2(:,2) = nearestpoint(B, A, 'after') ;
ans1 = A(ans2) % will fail when elements of B do not fall between elements of A. Check for that!
My NEARESTPOINT utility can be downloaded from the File Exchange:

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by