MATLAB Answers

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

조회 수: 6(최근 30일)
Stephen Devlin
Stephen Devlin 9 Feb 2018
댓글: Stephen Devlin 10 Feb 2018
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?
close all
clear all
% *******************
% *******************
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]
box on
grid minor
% *******************
% *******************
% check the Thing you select as first Thing to test is not in the Used_ThingsArray
% *******************
IndexAllThingsInROI= data(:,3)<= data(1,3)+(NozPitch)
TotalThingsWithinRange=sum(IndexAllThingsInROI(:) == 1)
% *******************
for k=1:2:length(dataCopy)
if ismember(ThingSelected,Used_ThingsArray)==0 ;%has the Thing selected already been measured
matchingThing = setdiff(ThingsInRange,ThingSelected,'stable')'
%%Checks, unneeded to run normally
ThingsDistanceLength=sqrt((((matchingThing(1,3)-ThingSelected(1,3))).^2) + (((matchingThing(1,2)-ThingSelected(1,2))).^2))/1000;
%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))))

  댓글 수: 4

표시 이전 댓글 수: 1
Stephen Devlin
Stephen Devlin 9 Feb 2018
Hi Geoff, Yes to MatchingThing. It doubles in length for k=3. and k=5, not taken it any further than that.
Guillaume 9 Feb 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 9 Feb 2018
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.

로그인 to comment.

채택된 답변

Greg 9 Feb 2018
편집: Greg 9 Feb 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

Stephen Devlin
Stephen Devlin 10 Feb 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,

로그인 to comment.

추가 답변(0개)

이 질문에 답변하려면 로그인을(를) 수행하십시오.

Translated by