For _positive_ integers:
a=A(:);
b=B(:);
m=max([a;b]);
x1=zeros(m,1);
x2=zeros(m,1);
x1(a)=ones(length(a),1);
x2(b)=ones(length(b),1);
find(x1&x2)
Regards, Michel.
>In article <4u7km0$2...@mo6.rc.tudelft.nl>, edwin@dutnak3 (Edwin Verheijen) writes:
>>I need to retrieve those elements that are shared in matrices
>>A en B without a time-consuming for-loop. Is it possible
>>to do this in one or two lines?
>>Example:
>>A=[1 6 9; 0 7 7; 2 8 2];
>>B=[0 9 7 3; 0 3 5 4];
>>
>>answer= 0 9 7
>>
>>In my case all elements are positive integers. An answer like
>>9 9 9 7 0 or whatever order or redundancy is no problem.
>>
Try:
A=[1 6 9; 0 7 7; 2 8 2];
B=[0 9 7 3; 0 3 5 4];
A=[A;A] % make more interesting set
B=[B;B] % make more interesting set
as=sort(A(:)); % sort list
asd=diff(as);
asi=find(asd==0);
as(asi)=[]; % discard duplicates
bs=sort(B(:)); % sort list
bsd=diff(bs);
bsi=find(bsd==0);
bs(bsi)=[]; % discard duplicates
abs=sort([as;bs]); % sort combined list
abi=find(diff(abs)==0);
abs(abi) % extract duplicates
Pete Britt
pe...@electrosystems.com
answer= 0 9 7
In my case all elements are positive integers. An answer like
9 9 9 7 0 or whatever order or redundancy is no problem.
Thanks in advance!
Edwin
--
-----------------------------------------------------------------
-- Edwin Verheijen ed...@akst.tn.tudelft.nl --
------ Laboratory of Seismics and Acoustics phone: (+31 15)2782021 ------
-- Delft University of Technology The Netherlands --
-----------------------------------------------------------------
--
-----------------------------------------------------------------
-- Edwin Verheijen ed...@akst.tn.tudelft.nl --
------ Laboratory of Seismics and Acoustics phone: (+31 15)2782021 ------
-- Delft University of Technology The Netherlands --
-----------------------------------------------------------------
> I need to retrieve those elements that are shared in matrices
> A en B without a time-consuming for-loop. Is it possible
> to do this in one or two lines?
> Example:
> A=[1 6 9; 0 7 7; 2 8 2];
> B=[0 9 7 3; 0 3 5 4];
>
> answer= 0 9 7
>
> In my case all elements are positive integers. An answer like
> 9 9 9 7 0 or whatever order or redundancy is no problem.
>
>
I haven't tried to clean up this code, but I bet you can shorten it.
a = A(:)'; % make A a row
b = B(:)'; % make B a row
maxlen = max(max(a),max(b))+1; % find maximum # entries (add one because of 0)
s = zeros(2,maxlen);
s(1,sort(a)+1) = ones(size(a)); % fill first row with a 1 for elements in a
s(2,sort(b)+1) = ones(size(b)); % fill second row with a 1 for elements in b
dupes = find(sum(s) == 2) - 1; % find indices common to a and b, subtract 1
% because of 0 base before (added 1 earlier)
dupes =
0 7 9
==== Loren Shure ========================= lo...@mathworks.com ====
The MathWorks, Inc. in...@mathworks.com
24 Prime Park Way http://www.mathworks.com
Natick, MA 01760-1500 ftp.mathworks.com
==== Tel: 508-647-7000 ===================== Fax: 508-647-7001 ====
This can be done with a neat application of sparse matrices -
a = A(:)'+1;
b = B(:)'+1;
simple = [find(sparse(1,a,1)>0);find(sparse(1,b,1)>0)];
values = find(full(sparse(1,simple',1))>1)-1
and voila ...
values =
0 7 9
--
JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
S Jeff Baggett office: (607)255-4195 S
S Center for Applied Math. home: (607)256-6894 S
S 657 Frank H.T. Rhodes Hall S
S Cornell University bag...@cam.cornell.edu S
S Ithaca, NY 14850 S
S S
S http://cam.cornell.edu/~baggett/index.html S
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
>This can be done with a neat application of sparse matrices -
>a = A(:)'+1;
>b = B(:)'+1;
>simple = [find(sparse(1,a,1)>0);find(sparse(1,b,1)>0)];
>values = find(full(sparse(1,simple',1))>1)-1
This looks prety neat! I'm still learning how to use sparse matrix
effectively. But I did notice a problem with the solution: How would
you modify it to handle other data sets with negative entries? e.g.,
A=A-2 & B=B-2.
A=[1 6 9; 0 7 7; 2 8 2];
B=[0 9 7 3; 0 3 5 4];
A=A-2
B=B-2
a = A(:)'+1;
b = B(:)'+1;
simple = [find(sparse(1,a,1)>0);find(sparse(1,b,1)>0)];
values = find(full(sparse(1,simple',1))>1)-1
results in:
Index into matrix is negative or zero
We were looking for
ans =
-2
5
7
Maybe I need a tutorial
Thanks,
Pete Britt
pe...@electrosysstems.com
Jeff, Thanks for the encouagement (via e-mail). My version of your
suggestion follows "your name in lights". Maybe I'll get it yet.
JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
S Jeff Baggett office: (607)255-4195 S
S Center for Applied Math. home: (607)256-6894 S
S 657 Frank H.T. Rhodes Hall S
S Cornell University bag...@cam.cornell.edu S
S Ithaca, NY 14850 S
S S
S http://cam.cornell.edu/~baggett/index.html S
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
% example
A=[1 6 9; 0 7 7; 2 8 2];
B=[0 9 7 3; 0 3 5 4];
A=A-2
B=B-2
% routine
a=round(A(:)); % to guarantee integer
b=round(B(:));
z=1-min([a;b]);
a = a(:)'+(z);
b = b(:)'+z;
simple = [find(sparse(1,a,1));find(sparse(1,b,1))];
values = find(full(sparse(1,simple',1))>1)-z
A =
-1 4 7
-2 5 5
0 6 0
B =
-2 7 5 1
-2 1 3 2
values =
-2 5 7
>Thanks,
>Pete Britt
>pe...@electrosysstems.com