How to do Subtraction on sets?

조회 수: 7 (최근 30일)
ramin bba
ramin bba 2014년 7월 6일
댓글: ramin bba 2014년 7월 7일
I have a continuous set (A=[0 , 2pi]) and want to subtract the following sets from it:
B= [0, pi/2];
C=[pi/4, pi];
D=[3pi/2, 7pi/4];
...
In general, I could just build the union of "B, C, ..." and then do the subtraction BUT I have to do this process a lot of times. In other words, the whole process is in a "for loop" and each time some sets need to be subtracted from A (each time the number of sets and their length is not necessarily might change). The initial interval (A) is the same for all the iterations (after each iteration I would get an interval corresponding to that iteration: Answer(i) = A - B(i) - C(i) -...).
I found a function in MATLAB called "setdiff" but it seems to only work on discrete intervals or images (I mean a 2D matrix)!
Is there a similar function for continuous intervals/sets?
Any help would be appreciated,
  댓글 수: 1
Image Analyst
Image Analyst 2014년 7월 6일
What's a continuous set? There is no continuous set of numbers in computers. If A is an array, then A has a certain, fixed and finite number of elements with certain definite values, so A must be discrete/digitized, not continuous.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2014년 7월 6일
Try this:
A = 0:360;
B = 0 : 60;
C = 45 : 60;
D = 270 : 360;
foundIndexesB = find(ismember(B, A))
foundIndexesC = find(ismember(C, A))
foundIndexesD = find(ismember(D, A))
allFoundIndexes = [foundIndexesB, foundIndexesC, foundIndexesD]
% Get rid of those numbers from A
A(allFoundIndexes) = [];
  댓글 수: 2
ramin bba
ramin bba 2014년 7월 6일
편집: Image Analyst 2014년 7월 6일
tnx a lot, it works but with a minor change:
foundIndexesB = find(ismember(A,B))
foundIndexesC = find(ismember(A,C))
foundIndexesD = find(ismember(A,D))
ramin bba
ramin bba 2014년 7월 7일
I used the method you outlined and it works. However, I am getting this suggestion from MATLAB and was wondering if you could elaborate on it a bit:
"if "A" is an indexed variable, performance can be improved using logical indexing instead of FIND.".
I know what is Logical indexing in general but I do not know how it can be used instead of "find".
tnx in advance

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Roger Stafford
Roger Stafford 2014년 7월 6일
You might consult these sites:
http://www.mathworks.com/matlabcentral/fileexchange/31753-range-intersection
http://www.mathworks.com/matlabcentral/fileexchange/24254-interval-merging
  댓글 수: 1
ramin bba
ramin bba 2014년 7월 6일
tnx for the answer Roger but the codes do not work. for example, if I have the following:
A=[0 360];
B=[0 60], C=[45 60], D=[270 360];
A-B-C-D=[60 27]
I even tried going one at a time (like below), but no success:
A-B=[60 360];
A-C=[0 45]+[60 360];
A-D=[0 270];
union{(A-B),(A-C),(A-D)}=[60 270];
They gave me though a rough idea of how to approach the problem.
tnx again,

댓글을 달려면 로그인하십시오.

카테고리

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