| Author |
matrix A excludes matrix B
|
|
| pangzi 2007-06-20, 7:20 pm |
| hail all:
Another question:
If I have a array A with 10 numbers in it and another array B with 5
numbers which are included in A.
How could I get the array C that is the array A excludes array B?
ex.
-
A=[1 2 3 4 5 6 7 8 9 10];
B=[1 4 5 6 10];
C=[2 3 7 8 9];
-
thanks very much
| |
|
| pangzi:
<SNIP membership evergreen...
one of the solutions
a=1:10;
b=1:2:10;
c=a(~ismember(a,b))
us
| |
| Walter Roberson 2007-06-20, 7:20 pm |
| In article <ef5b40a.-1@webcrossing.raydaftYaTP>,
pangzi <bbbbbh@hotmail.com> wrote:
>If I have a array A with 10 numbers in it and another array B with 5
>numbers which are included in A.
>How could I get the array C that is the array A excludes array B?
>A=[1 2 3 4 5 6 7 8 9 10];
>B=[1 4 5 6 10];
>C=[2 3 7 8 9];
C = setdiff(A,B);
Note: if you use setdiff, then the results will be sorted and will
not include any duplicates. (Your question becomes unclear if
duplicates are allowed in the matrices.)
--
Programming is what happens while you're busy making other plans.
| |
| Yi Cao 2007-06-20, 7:20 pm |
| us wrote:
>
>
> pangzi:
> <SNIP membership evergreen...
>
> one of the solutions
>
> a=1:10;
> b=1:2:10;
> c=a(~ismember(a,b))
>
> us
C=setdiff(A,B);
hth
Yi
| |
|
| Yi Cao:
<SNIP good alternative to <us>'s solutions
> C=setdiff(A,B);
but then... let's just see
a=1:10;
b=1:2:10;
tic;
for i=1:1000
c=a(~ismember(a,b));
end
toc
tic;
for i=1:1000
d=setdiff(a,b);
end
toc
with these numbers on a wintel
p5/1.4g/512m/winxp.sp2/r2007a
Elapsed time is 0.317095 seconds. % <- <ismember>
Elapsed time is 0.828321 seconds. % <- <setdiff>
now, why might that be?
<setdiff> calls <ismember> and <unique>...
just a thought
us
| |
| Yi Cao 2007-06-20, 7:20 pm |
| us wrote:
>
> now, why might that be?
> <setdiff> calls <ismember> and <unique>...
>
> just a thought
> us
It is true. setdiff returns unique elements of A which are not in B.
If there are no repeated elements in both A and B,
C=A(~ismember(A,B)) is the simplest solution.
hth
Yi
|
|
|
|