Add a vector to a matrix if satisfies one condition

2 views (last 30 days)
I have a random sorted matriz A of coordinates[x, y, z], sorted in the third column
A=
-3.7101 5.9258 9.9850
1.7114 1.9374 6.4446
0.4066 1.2890 3.9829
and another random vector B [x, y, z]
B=
-6.4932 3.3327 5.2881
I want to create a new matrix C, adding vector B to vector A (the position of the new vector does not matter):
C=
-3.7101 5.9258 9.9850
1.7114 1.9374 6.4446
0.4066 1.2890 3.9829
vector B--> -6.4932 3.3327 5.2881
BUT, I want to add the vector, if and only if the values in the third column satisfy the following condition:
6.44 - 1 > 5.2882 > 3.98 + 1
5.44 > 5.28 > 4.98
This means, I want to find in the matrix A the two closest values (in the third column) to B, and compare them in the condition. If the condition is accomplised, add the vector to the matrix. Thank you in advance I really appreciate your help.
I would like just to know how to find those two closest values to B(1,3)= in A and then test the condition.

Answers (1)

Bhaskar R
Bhaskar R on 5 Dec 2019
col_3 = A(:, 3); % 3rd column of A
con = col_3(2)- 1> B(end) >col_3(end)+1; % your condition
if cond
C = [A;B]; % concatanate A and B
end

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!