Add a vector to a matrix if satisfies one condition
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Bhaskar R
2019-12-5
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!