problem using setdiff to check if a vector is present in a loop

1 vue (au cours des 30 derniers jours)
Stephen Devlin
Stephen Devlin le 9 Fév 2018
Commenté : Stephen Devlin le 10 Fév 2018
Hi,
I have a problem with some code I have written and hope someone has a pointer or two. The issue is with the variable "matchingThing",derived from using setdiff on two different arrays. The problem is when I use it in a loop, on the second pass through the loop matching drop seems to double in length, taking the variables from the previous run through the loop instead of seeing the difference between the two current sets.
Any ideas?
Steve
close all
clear all
clc
% *******************
NozPitch=0.08406666;
Delay2=10.05;
delayTime=(Delay2/1000000);
% *******************
data= [ 1 15 12.162 0.488 6.897
2 15 12.165 0.581 7.016
3 15 12.343 0.578 6.913
4 15 12.366 0.522 6.79
5 15 12.535 0.605 6.805
6 15 12.537 0.461 6.991]
scatter(data(:,3),data(:,4),'filled','k')
box on
grid minor
% *******************
Used_ThingsArray=nan(length(data),5);
% *******************
dataCopy=data;
dataCopy(:,2)=[];
FrameThingCoords=dataCopy
% check the Thing you select as first Thing to test is not in the Used_ThingsArray
selectThing=dataCopy(1,:)
% *******************
IndexAllThingsInROI= data(:,3)<= data(1,3)+(NozPitch)
TotalThingsWithinRange=sum(IndexAllThingsInROI(:) == 1)
ThingsInRange=dataCopy(IndexAllThingsInROI,:)
% *******************
for k=1:2:length(dataCopy)
%
Thing=k
ThingSelected=dataCopy(k,:)
if ismember(ThingSelected,Used_ThingsArray)==0 ;%has the Thing selected already been measured
ThingSelectedCoords=ThingSelected(:,2:3)
matchingThing = setdiff(ThingsInRange,ThingSelected,'stable')'
%%Checks, unneeded to run normally
CheckThingsInRange{k}=ThingsInRange
CheckThingSelected{k}=ThingSelected
CheckmatchingThing{k}=matchingThing
ThingsDistanceLength=sqrt((((matchingThing(1,3)-ThingSelected(1,3))).^2) + (((matchingThing(1,2)-ThingSelected(1,2))).^2))/1000;
Velocity=ThingsDistanceLength/delayTime
%trajectory_mR=(tan((matchingThing(1,2)-ThingSelected(1,2) ) /( matchingThing(1,3)-ThingSelected(1,3))))*1000%mrads
trajectory_D=(tand((matchingThing(1,2)-ThingSelected(1,2) ) /( matchingThing(1,3)-ThingSelected(1,3))))
end
end
  4 commentaires
Guillaume
Guillaume le 9 Fév 2018
Modifié(e) : Guillaume le 9 Fév 2018
I personally find the code very confusing with no explanation why sometimes data being used and other times datacopy, one being the other with one less column.
Never ever use length on a matrix. In your case, you probably mean size(matrix, 1). Be aware that length could return size(matrix, 2). However, I doubt it's the reason for your problem.
In fact, never ever use length. Use numel for vector and size with an explicit dimension for matrices.
A common reason for arrays not being the correct size is the lack of preallocation and the growing of arrays in a loop.
I haven't looked at the code in enough details to understand the issue. I would recommend reducing the number of variables to make the code easier to understand. datacopy and and selectThing serve no purpose at all. datacopy(r, c) is either data(r, c) for c == 1 or data(r, c+1) for c >= 1.
Stephen Devlin
Stephen Devlin le 9 Fév 2018
Hi,
I don’t have a computer nearby, just my phone. dataCopy is just a copy of data with a column taken out but I wanted to preserve the original data for when I get past the problem I have at the moment.

Connectez-vous pour commenter.

Réponse acceptée

Greg
Greg le 9 Fév 2018
Modifié(e) : Greg le 9 Fév 2018
I think all you need is the 'rows' input to setdiff. Otherwise, setdiff includes an implicit unique, so any duplicate data points in ThingsInRange will be removed, regardless of their appearance in ThingSelected.
matchingThing = setdiff(ThingsInRange,ThingSelected,'stable','rows');
  1 commentaire
Stephen Devlin
Stephen Devlin le 10 Fév 2018
Hi Greg, yes just tried that and removed the apostrophe that made the output into a column vector, now works perfectly.
Very much appreciated.
Have a good weekend,
Steve

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by