Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: comparing columns each element

Subject: comparing columns each element

From: Katie

Date: 12 Jun, 2008 13:16:01

Message: 1 of 7

I have a cell array R=(4096,2). I need to compare each
element (in this case the elements are 4x4 matrices) in the
first column to each element in the 2nd. If a matrix in the
2nd column is the same as one in the first then i need to
change the matrix in the 2nd column to the zero matrix.
How can I do this? (I have tried creating a loop but it
only compares matrices that are in the same row.) I also
have them in seperate cell arrays. Would it be easier to
compare them that way?
thanks!

Subject: comparing columns each element

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 12 Jun, 2008 15:49:41

Message: 2 of 7

In article <g2r7ih$550$1@fred.mathworks.com>,
Katie <kmcdon03@uoguelph.ca> wrote:
>I have a cell array R=(4096,2). I need to compare each
>element (in this case the elements are 4x4 matrices) in the
>first column to each element in the 2nd. If a matrix in the
>2nd column is the same as one in the first then i need to
>change the matrix in the 2nd column to the zero matrix.
>How can I do this? (I have tried creating a loop but it
>only compares matrices that are in the same row.) I also
>have them in seperate cell arrays. Would it be easier to
>compare them that way?

I think this should work:

R(equal(R(:,1),R(:,2)),2) = {zeros(4,4)};

--
  "Not the fruit of experience, but experience itself, is the end."
                                              -- Walter Pater

Subject: comparing columns each element

From: Katie

Date: 12 Jun, 2008 18:32:01

Message: 3 of 7



> R(equal(R(:,1),R(:,2)),2) = {zeros(4,4)};

i tried this and it came up with this error message:

??? Undefined function or method 'equal' for input arguments
of type 'cell'.

thanks for the help!

Subject: comparing columns each element

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 12 Jun, 2008 19:03:08

Message: 4 of 7

In article <g2rq31$bgp$1@fred.mathworks.com>,
Katie <kmcdon03@uoguelph.ca> wrote:

>
>> R(equal(R(:,1),R(:,2)),2) = {zeros(4,4)};

>i tried this and it came up with this error message:

>??? Undefined function or method 'equal' for input arguments
>of type 'cell'.

Sorry, that should have been isequal() instead of equal()
--
  "The whole history of civilization is strewn with creeds and
  institutions which were invaluable at first, and deadly
  afterwards." -- Walter Bagehot

Subject: comparing columns each element

From: us

Date: 12 Jun, 2008 20:04:02

Message: 5 of 7

"Katie ":
<SNIP looking for equal contents

one of the many solutions

% the data
     m=1:3;
     c={
          m,m
          m,-m
          m,m
          m,-m
     };
% the engine
     ix=cellfun(@(x,y) isequal(x,y),c(:,1),c(:,2));
     c(ix,2)={0*m};
% the result
     cat(1,c{:,2})
%{
     only shows the contents of the col #2
     ans =
      0 0 0
     -1 -2 -3
      0 0 0
     -1 -2 -3
%}

us

Subject: comparing columns each element

From: Katie

Date: 16 Jun, 2008 12:37:01

Message: 6 of 7

thank you !


Subject: comparing columns each element

From: Katie

Date: 16 Jun, 2008 15:48:01

Message: 7 of 7

thank you!
could you send another one of the many solutions?
I am still not sure how to use that one.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
code us 12 Jun, 2008 16:05:08
cell us 12 Jun, 2008 16:05:08
cellfun us 12 Jun, 2008 16:05:08
cat us 12 Jun, 2008 16:05:08
logical indexing us 12 Jun, 2008 16:05:08
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics