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:
effective way to compare two Matrixes whose dimensions do not agree?

Subject: effective way to compare two Matrixes whose dimensions do not agree?

From: Feng

Date: 27 Apr, 2009 15:38:01

Message: 1 of 7

I met a problem, want to compare two matrixes whose dimensions do not agree.
I can do it using loop, but I just wondering if there is better/faster way to do that.

For example, I have a matrix A(5x5), and Matrix B(1x4), the thing I want to to do is if the element in A also is element in B, I want to set this element in A to 0, so after that, I will get a new matrix A'(5x5), whose elements are shown in B before now are zero.

Any suggest will be appreciated.

Thanks

Subject: effective way to compare two Matrixes whose dimensions do not agree?

From: Steven Lord

Date: 27 Apr, 2009 17:30:10

Message: 2 of 7


"Feng" <nclxin@hotmail.com> wrote in message
news:gt4jgp$8vr$1@fred.mathworks.com...
>I met a problem, want to compare two matrixes whose dimensions do not
>agree.
> I can do it using loop, but I just wondering if there is better/faster way
> to do that.
>
> For example, I have a matrix A(5x5), and Matrix B(1x4), the thing I want
> to to do is if the element in A also is element in B, I want to set this
> element in A to 0, so after that, I will get a new matrix A'(5x5), whose
> elements are shown in B before now are zero.
>
> Any suggest will be appreciated.

Use logical indexing with the ISMEMBER function -- or for such a short B
vector just loop over its elements.

--
Steve Lord
slord@mathworks.com

Subject: effective way to compare two Matrixes whose dimensions do not agree?

From: Matt

Date: 27 Apr, 2009 18:59:02

Message: 3 of 7

"Feng" <nclxin@hotmail.com> wrote in message <gt4jgp$8vr$1@fred.mathworks.com>...
> I met a problem, want to compare two matrixes whose dimensions do not agree.
> I can do it using loop, but I just wondering if there is better/faster way to do that.
>
> For example, I have a matrix A(5x5), and Matrix B(1x4), the thing I want to to do is if the element in A also is element in B, I want to set this element in A to 0, so after that, I will get a new matrix A'(5x5), whose elements are shown in B before now are zero.
------

[c,ia]=intersect(A(:),B(:));

A(ia)=0;

Subject: effective way to compare two Matrixes whose dimensions do not agree?

From: Feng

Date: 27 Apr, 2009 19:35:03

Message: 4 of 7

Hi, Matt, thanks for your help

if A = [0 1 2 0 3
          1 0 0 4 0
          2 0 0 5 0
          0 4 5 0 6
          3 0 0 6 0];

B = [1 2 3 4]

I want to get A' = [0 0 0 0 0
                            0 0 0 0 0
                            0 0 0 5 0
                            0 0 5 0 6
                            0 0 0 6 0];

[c, ia] = intersect(A(:), B(:))

c =

     1
     2
     3
     4


ia =

     6
    11
    21
    17

A(ia) = [0 0 0 0 0
     1 0 0 0 0
     2 0 0 5 0
     0 4 5 0 6
     3 0 0 6 0]

That is not what I want
"Matt " <xys@whatever.com> wrote in message <gt4v9m$g2t$1@fred.mathworks.com>...
> "Feng" <nclxin@hotmail.com> wrote in message <gt4jgp$8vr$1@fred.mathworks.com>...
> > I met a problem, want to compare two matrixes whose dimensions do not agree.
> > I can do it using loop, but I just wondering if there is better/faster way to do that.
> >
> > For example, I have a matrix A(5x5), and Matrix B(1x4), the thing I want to to do is if the element in A also is element in B, I want to set this element in A to 0, so after that, I will get a new matrix A'(5x5), whose elements are shown in B before now are zero.
> ------
>
> [c,ia]=intersect(A(:),B(:));
>
> A(ia)=0;

Subject: effective way to compare two Matrixes whose dimensions do not agree?

From: Matt

Date: 27 Apr, 2009 20:40:20

Message: 5 of 7

"Feng" <nclxin@hotmail.com> wrote in message <gt51d7$c8j$1@fred.mathworks.com>...
> Hi, Matt, thanks for your help
>
> if A = [0 1 2 0 3
> 1 0 0 4 0
> 2 0 0 5 0
> 0 4 5 0 6
> 3 0 0 6 0];
>
> B = [1 2 3 4]
>
> I want to get A' = [0 0 0 0 0
> 0 0 0 0 0
> 0 0 0 5 0
> 0 0 5 0 6
> 0 0 0 6 0];
>
> [c, ia] = intersect(A(:), B(:))
>
> c =
>
> 1
> 2
> 3
> 4
>
>
> ia =
>
> 6
> 11
> 21
> 17
>
> A(ia) = [0 0 0 0 0
> 1 0 0 0 0
> 2 0 0 5 0
> 0 4 5 0 6
> 3 0 0 6 0]
>
> That is not what I want


Hmmm. Looks like ismember is the way to go, as Steve suggested.

Subject: effective way to compare two Matrixes whose dimensions do not agree?

From: Feng

Date: 27 Apr, 2009 20:59:02

Message: 6 of 7

"Matt " <xys@whatever.com> wrote in message <gt557k$4j8$1@fred.mathworks.com>...
> "Feng" <nclxin@hotmail.com> wrote in message <gt51d7$c8j$1@fred.mathworks.com>...
> > Hi, Matt, thanks for your help
> >
> > if A = [0 1 2 0 3
> > 1 0 0 4 0
> > 2 0 0 5 0
> > 0 4 5 0 6
> > 3 0 0 6 0];
> >
> > B = [1 2 3 4]
> >
> > I want to get A' = [0 0 0 0 0
> > 0 0 0 0 0
> > 0 0 0 5 0
> > 0 0 5 0 6
> > 0 0 0 6 0];
> >
> > [c, ia] = intersect(A(:), B(:))
> >
> > c =
> >
> > 1
> > 2
> > 3
> > 4
> >
> >
> > ia =
> >
> > 6
> > 11
> > 21
> > 17
> >
> > A(ia) = [0 0 0 0 0
> > 1 0 0 0 0
> > 2 0 0 5 0
> > 0 4 5 0 6
> > 3 0 0 6 0]
> >
> > That is not what I want
>
>
> Hmmm. Looks like ismember is the way to go, as Steve suggested.

ismember is good for my purpose. One thing I noticed that the efficiency of 'ismember' is not good comparing with the function 'strmatch'.

rowNum = strmatch(round(temp),round(A),'exact');
rowNum = ismember(A, temp,'rows');
( here A is a matrix, temp is a vector, want to find if temp is a row in A. )

Subject: effective way to compare two Matrixes whose dimensions do not agree?

From: Image Analyst

Date: 27 Apr, 2009 23:11:01

Message: 7 of 7

"Feng":
Here is a pretty straightforward and easy to understand way of doing it:

A = [0 1 2 0 3 ;...
     1 0 0 4 0 ;...
     2 0 0 5 0 ;...
     0 4 5 0 6 ;...
     3 0 0 6 0]

B = [1 2 3 4]
% I want to get A' = [0 0 0 0 0
% 0 0 0 0 0
% 0 0 0 5 0
% 0 0 5 0 6
% 0 0 0 6 0];

% Convert the values.
newValue = 0; % Whatever you want to change it to.
for k = 1:length(B)
A(A == B(k)) = newValue;
end
A % Display A (will be the same as A' that Feng listed above)

Good luck,
ImageAnalyst

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