필터 지우기
필터 지우기

Finding nearest values in two matrices

조회 수: 17 (최근 30일)
John Pickles
John Pickles 2019년 4월 4일
편집: per isakson 2019년 4월 4일
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!

채택된 답변

Adriano Filippo Inno
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개)

카테고리

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