Use conditional statements to separate elements from a single array into 2 new arrays

1 次查看(过去 30 天)
Hello, I am trying to separate elements from a matrix A (10x2) using conditional statements. I compare the distances between the all the points in A and if they are smaller than the threshold (16) then the pair of points that created that produced that result are isolated in another matrix (HS_elim). I want to use that newly created elim matrix to determine the points that wont be eliminated and instead sent to another matrix (B). I am able to isolated the points that meet my if condition but I cannot isolate the points that fail the condition. the snippet of code I am using is below. Any help would be greatly appreciated.
HS_HS_threshold = 16;
for i = 1:n
for j = 1:n
elim_dist2(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2);
if (elim_dist2(i,j) ~= 0 && elim_dist2(i,j) < HS_HS_threshold)
HS_elim_x = x(j); HS_elim_y = y(j);
HS_elim(j,:) = [HS_elim_x, HS_elim_y];
elseif HS_elim(:,1) ~= x(i) && HS_elim(:,2) ~= y(i)
keep_x = x(i); keep_y = y(i);
B(i,:) = [keep_x, keep_y];
end
end
end
  3 个评论
Walter Roberson
Walter Roberson 2019-8-15
If HS_elim has more than one row then the && will fail because && requires scalars on both sides of the &&
Vance Blake
Vance Blake 2019-8-16
编辑:Vance Blake 2019-8-16
@Walter Oh so is that why the second matrix keeps collecting all the points instead of just the points that aren't too close to each other??
@Geoff I tried else first but the outcome was still incorrect because of what Walter explained if Im understanding correctly.

请先登录,再进行评论。

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2019-8-16
Are you trying to separate points that have no neighbours closer than HS_HS_threshold from those that have?
If so this should do something like that:
elim_dist2 = nan(numel(x)); % this to have nans on the diagonal after distance calculation
HS_HS_threshold = 16;
for i = n:-1:1 % looping from largest index lets you avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix
for j = (i-1):-1:1 % distance symmetric (l(i1,i2) == l(i2,i1)) so calculate it once
elim_dist2(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2);
elim_dist2(j,i) = elim_dist2(i,j);
end
end
% find the points that have its nearest neighbour further away than HS_HS_threshold:
i2keep = find(nanmin(elim_dist2)>HS_HS_threshold);
% put those into one pair of arrays
keep_x = x(i2keep);
keep_y = y(i2keep);
% and the others into another pair of arrays
x_close_neighbours = x;
y_close_neighbours = y;
x_close_neighbours(i2keep) = [];
y_close_neighbours(i2keep) = [];
HTH
  6 个评论
Guillaume
Guillaume 2019-8-28
None of the pieces of code you were given assume anything about the size of the inputs x and y (other than they are vectors). For a vector of N points, all the example will give you a NxN matrix for elim_dist2.
If that's not the case for you, then you're doing something different. It's clearly different since your original code worked with two vectors of the same length and you're now telling us that you have two matrices of different size instead. Please clarify what you're doing by showing us the actual code.
Vance Blake
Vance Blake 2019-8-28
Oh I acutally figured out my problem. I needed to set up the initial NaN matrix for the bigger of the 2 sets of vectors not matrices (my bad on the wrong terminology) and then fill out the resulting matrix with the distance values accordingly. Therefore avoidng the problem of comparing vectors of unequal size and gettting 0s. Basically i didnt realize just how good of a solution you guys gave me. thanks for the help both now and then.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by