Finding nearest values in two matrices

10 次查看(过去 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!

采纳的回答

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