Finding nearest values in two matrices
조회 수: 22 (최근 30일)
이전 댓글 표시
Hi all,
I have two matrices of diffent size, e.g. A= n x 3 and B = m x 3. The first column for both of them represents the depth taken by downhole survey.
If I show you soe part from two matrices, it can be something like:
A: B:
100 0 0 100 0 0
125 1 1 149 1.9 1.9
150 2 2 181 3.2 3.2
175 3 3 200 4.4 4.4
200 4 4 ...
...
It is, the records are taken at slightly different depths and might less/more frequent.
I would like to work based on the depth of the shortest matrix (let's say it's B). I need to create a loop perhaps, so it reads every depth input in the matrix and finds the closest value in the longest matrix. For example, for i =1, corresponding value of 100 (B) would be also 100 (A). For i = 2, corresponding to 149 (B) would be 150 (A), and so on until it ends, saving the corresponding values in columns 2 & 3.
In the end it would generate me a shorter version of matrix A, for every index matching the value in B, such that:
A: B:
100 0 0 100 0 0
150 2 2 149 1.9 1.9
175 3 3 181 3.2 3.2
200 4 4 200 4.4 4.4
...
Can be two separate matrices as above or combine into one C = m x 6.
I would appreciate some help. Thanks!
댓글 수: 0
채택된 답변
Adriano Filippo Inno
2019년 4월 4일
Hi John, the following do what you asked using min:
a = A(:,1);
b = B(:,1);
Nb = length(b);
A_new = zeros(Nb,3);
for i = 1:Nb
[~,index] = min(abs(a - b(i)));
A_new(i,:) = A(index,:);
end
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!