Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Comparing matrix to matrix

Subject: Comparing matrix to matrix

From: Ho Cheung Brian

Date: 14 Mar, 2012 05:29:24

Message: 1 of 4

Hi all experienced matlab user,

Here is my problem:

I want to make it faster, so I don't want to use any for loop..(Since I want to compete with CPlex..I hope it is feasible :( )
Description:

Input:
A =[1 6 3; 2 4 6;4 5 3];
B = [1 3 5;2 7 4];

Output:
[1 6 3; 4 5 3; 1 3 5]
(Order is not important)

**My goal is merging A and B and eliminate the non-dominated vector**
i.e. A(2,:) and B(2,:)
(The elements of [1 3 5] are all smaller than [2 4 6], so [2 4 6] is dominated by [1 3 5], similar to [2 7 4], which is dominated by [1 6 3])


Here is my current MATLAB Code:

%I compare each element of A to the element of B to find A's dominated solution:
A =[1 6 3; 2 4 6;4 5 3];
B = [1 3 5;2 7 4];

for iteration = 1: size(B,1)
i = find(B(iteration,1) < A(:,1));
j = find(B(iteration,2) < A(i,2));
k = find(B(iteration,3) < A(i(j),3));

if isempty(k) ~=1
A(i(j(k)),:) = []; %delete the vector that is not dominated by others.
end

end



%In reverse, I compare each element of B to the element of A to find B's dominated solution:

for iteration = 1: size(A,1)
i = find(A(iteration,1) < B(:,1));
j = find(A(iteration,2) < B(i,2));
k = find(A(iteration,3) < B(i(j),3));

if isempty(k) ~=1
B(i(j(k)),:) = []; %delete the vector that is not dominated by others.
end

end


%End

Does anyone know how to compare the matrix with different size by using find function??

Thanks!

Subject: Comparing matrix to matrix

From: Roger Stafford

Date: 14 Mar, 2012 07:29:11

Message: 2 of 4

"Ho Cheung Brian" wrote in message <jjpabk$t0j$1@newscl01ah.mathworks.com>...
> Input:
> A =[1 6 3; 2 4 6;4 5 3];
> B = [1 3 5;2 7 4];
> Output:
> [1 6 3; 4 5 3; 1 3 5]
> (Order is not important)
> **My goal is merging A and B and eliminate the non-dominated vector**
> i.e. A(2,:) and B(2,:)
> (The elements of [1 3 5] are all smaller than [2 4 6], so [2 4 6] is dominated by [1 3 5], similar to [2 7 4], which is dominated by [1 6 3])
> ........
- - - - - - - - -
  I hope I have understood your problem correctly. Your use of the phrase "dominated by" is a bit misleading.

 A1 = reshape(A,size(A,1),1,[]);
 B1 = reshape(B,1,size(B,1),[]);
 output = [A(~any(all(bsxfun(@gt,A1,B1),3),2),:);...
           B(~any(all(bsxfun(@gt,B1,A1),3),1),:)];

Roger Stafford

Subject: Comparing matrix to matrix

From: Ho Cheung Brian

Date: 14 Mar, 2012 08:12:12

Message: 3 of 4

Thanks. It's just very helpful!

Let me restate the dominance relationship
If a = (a1,a2,a3...,ak) and it is non-dominated, then there does not exist any b = (b1,b2,b3...,bk) such that b1<=a1, b2<=a2...,bk<=ak.

Thank you again.
"Roger Stafford" wrote in message <jjphc7$jcu$1@newscl01ah.mathworks.com>...
> "Ho Cheung Brian" wrote in message <jjpabk$t0j$1@newscl01ah.mathworks.com>...
> > Input:
> > A =[1 6 3; 2 4 6;4 5 3];
> > B = [1 3 5;2 7 4];
> > Output:
> > [1 6 3; 4 5 3; 1 3 5]
> > (Order is not important)
> > **My goal is merging A and B and eliminate the non-dominated vector**
> > i.e. A(2,:) and B(2,:)
> > (The elements of [1 3 5] are all smaller than [2 4 6], so [2 4 6] is dominated by [1 3 5], similar to [2 7 4], which is dominated by [1 6 3])
> > ........
> - - - - - - - - -
> I hope I have understood your problem correctly. Your use of the phrase "dominated by" is a bit misleading.
>
> A1 = reshape(A,size(A,1),1,[]);
> B1 = reshape(B,1,size(B,1),[]);
> output = [A(~any(all(bsxfun(@gt,A1,B1),3),2),:);...
> B(~any(all(bsxfun(@gt,B1,A1),3),1),:)];
>
> Roger Stafford

Subject: Comparing matrix to matrix

From: Roger Stafford

Date: 14 Mar, 2012 08:34:19

Message: 4 of 4

"Ho Cheung Brian" wrote in message <jjpjss$qhb$1@newscl01ah.mathworks.com>...
> Let me restate the dominance relationship
> If a = (a1,a2,a3...,ak) and it is non-dominated, then there does not exist any b = (b1,b2,b3...,bk) such that b1<=a1, b2<=a2...,bk<=ak.
- - - - - - - -
  Most people would say a = (a1,a2,a3...,ak) is non-dominated if there does not exist a b = (b1,b2,b3...,bk) such that a1<b1, a2<b2, etc. The word 'dominate' usually carries with it the concept of being greater, bigger, more than, overbearing to, superior to, other entities. This is the opposite of the notion of being less than.

  However in my code I have carried out what I thought you wanted.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us