×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

compare matrix elements

compare matrix elements

compare matrix elements

(OP)
Hi all. I'm new in matlab and i want to do smth like that:

Having two matrixes(vectors is better), like

A=[1;2;6;3;5;8]
B=[4;6;9;7;4;5]

I want to compare all elements of A to B, and if an element of A is same with one of B (not in same position necessarilly) to be written in another vector. In this example, i want to create a vector like

C=[6;5]

I spent many hours reading help documentation on matrixes but to no avail. I found smth like 'any' but did not understand it's functionality, and especially if it feets my needs.

Is this ....SO difficult...? I do not suppose so, but is there anyone here available to show me how?

Thanx in advance.

RE: compare matrix elements

Hi, the first thing I think to is to use for loops to control all position in both matrices.

[ra ca]=size(A);
[rb cb]=size(B);
t=1;
for i=1:ra
  for j=1:ca
    for k=1:rb
      for l=1:cb
        if A(i,j)==B(k,l)
          C(t)=A(i,j);
          t=t+1;
        end;
      end;
    end;
  end;
end;

There is another way, using function 'find':
[ra ca]=size(A);
[rb cb]=size(B);
t=1;
for i=1:ra
  for j=1:ca
    if isempty(find(B==A(i,j)))==0
      C(t)=A(i,j);
      t=t+1;
    end;
  end;
end;

Function find returns every value and index sattisfies the condition between brackets. If condition is not satisfied returns empty value. 'isempty' returns 1 if inside its brackets there is empty value.
I don't know which one is faster, because I think that behind function find there are for loops (and mabye for loops will be more direct). Only in case you work with larger matrixs.

Good luck

RE: compare matrix elements

Jeez, that has to be a record for the greatest number of redundant "for" loops!

What is wrong with this

A=[1;2;6;3;5;8]
B=[4;6;9;7;4;5]
C=intersect(A,B)

M

--
Dr Michael F Platten

RE: compare matrix elements

(OP)
Thank you both. I will check for "help intersect" on the command window first and try some of the "for" loops as well, just to get acquainted with them. Thank you both ppl.

RE: compare matrix elements

If A and B are the same length (and not too long!), this obfuscated one-liner will also work.  I'm not suggesting it's a good solution, but one that'll teach you a thing or two about Matlab.

B(sum(perms(A)==B(:,ones(1,factorial(length(A))))')>0)

RE: compare matrix elements

(OP)
B(sum(perms(A)==B(:,ones(1,factorial(length(A))))')>0)

At this point, the only thing i understand in the line above, is 'sum' (lol). It seems that i have to do a lot of reading!

Oh, i can also understand 'length' :P

(grateful)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources