Use conditional statements to separate elements from a single array into 2 new arrays
Afficher commentaires plus anciens
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 commentaires
Geoff Hayes
le 15 Août 2019
Vance - what is the intent behind this condition
HS_elim(:,1) ~= x(i) && HS_elim(:,2) ~= y(i)
You are comparing two columns with (presumably) scalars x and y. If you just want to isolate the points that fail the condition then why not replace the elseif with an else?
Walter Roberson
le 15 Août 2019
If HS_elim has more than one row then the && will fail because && requires scalars on both sides of the &&
Vance Blake
le 16 Août 2019
Modifié(e) : Vance Blake
le 16 Août 2019
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!