Thread Subject: for loop and comparisons

Subject: for loop and comparisons

From: KA

Date: 17 Jun, 2008 14:15:05

Message: 1 of 4

hi all,

I am new to Matlab, so please bare with me.
I have 2 cell arrays and I want to compare each element in
one to each element in the other. However, when I create a
for loop like this
(as an example)
i=1:100
if A{i}==2*B{i}, B{i}=[], end
end
It only compares the 1st element to the 1st, the 2nd to the
2nd and so on.
Is there a way to change this, or another approach that I
should take?
thank you in advance, any help is appreciated!


Subject: for loop and comparisons

From: French Caro

Date: 17 Jun, 2008 14:21:01

Message: 2 of 4

"KA " <kmcdon03@uoguelph.ca> wrote in message
<g38gt9$h35$1@fred.mathworks.com>...
> hi all,
>
> I am new to Matlab, so please bare with me.
> I have 2 cell arrays and I want to compare each element in
> one to each element in the other. However, when I create a
> for loop like this
> (as an example)
> i=1:100
> if A{i}==2*B{i}, B{i}=[], end
> end
> It only compares the 1st element to the 1st, the 2nd to the
> 2nd and so on.
> Is there a way to change this, or another approach that I
> should take?
> thank you in advance, any help is appreciated!
>
Hi
you have to do a double loop (maybe there is a convenient
way to vectorize but if you're new it's better to do it this
way first)

for i=1:length(A)
   for j=1:lenth(B)
      if A{i}==2*B{j}
          B{j}=[];%See Note
      end
end

Note : if you do this you won't compare B{j} anymore...

Note2 : to do == comparison be sure that each A{i} and B{i}
are scalars (so why using cell array here instead of matrix ?)

Caroline


Subject: for loop and comparisons

From: Jos

Date: 17 Jun, 2008 14:25:04

Message: 3 of 4

"KA " <kmcdon03@uoguelph.ca> wrote in message
<g38gt9$h35$1@fred.mathworks.com>...
> hi all,
>
> I am new to Matlab, so please bare with me.
> I have 2 cell arrays and I want to compare each element in
> one to each element in the other. However, when I create a
> for loop like this
> (as an example)
> i=1:100
> if A{i}==2*B{i}, B{i}=[], end
> end
> It only compares the 1st element to the 1st, the 2nd to the
> 2nd and so on.
> Is there a way to change this, or another approach that I
> should take?
> thank you in advance, any help is appreciated!
>
>

Given the statement 2 * B{i} I assume that each cell of A
and B holds a double array?
Are all these arrays the same size?
Can you convert them into one big matrix using CAT?

If so, use RESHAPE to make them a 2D array with each row
holding one cell of the original arrays, and use ISMEMBER or
SETDIFF with the rows option, to look for similaraties
and/or differences between A and B.


In pseudocode:

A2 = cat(..,A{:}) ;
A3 = reshape(A2,...)

C = setdiff(A3,B3,'rows')

hth
Jos

Subject: for loop and comparisons

From: KA

Date: 17 Jun, 2008 16:01:03

Message: 4 of 4

"Jos " <DELjos@jasenDEL.nl> wrote in message
<g38hg0$nlb$1@fred.mathworks.com>...
> "KA " <kmcdon03@uoguelph.ca> wrote in message
> <g38gt9$h35$1@fred.mathworks.com>...
> > hi all,
> >
> > I am new to Matlab, so please bare with me.
> > I have 2 cell arrays and I want to compare each element in
> > one to each element in the other. However, when I create a
> > for loop like this
> > (as an example)
> > i=1:100
> > if A{i}==2*B{i}, B{i}=[], end
> > end
> > It only compares the 1st element to the 1st, the 2nd to the
> > 2nd and so on.
> > Is there a way to change this, or another approach that I
> > should take?
> > thank you in advance, any help is appreciated!
> >
> >
>
> Given the statement 2 * B{i} I assume that each cell of A
> and B holds a double array?
> Are all these arrays the same size?
> Can you convert them into one big matrix using CAT?
>
> If so, use RESHAPE to make them a 2D array with each row
> holding one cell of the original arrays, and use ISMEMBER or
> SETDIFF with the rows option, to look for similaraties
> and/or differences between A and B.
>
>
> In pseudocode:
>
> A2 = cat(..,A{:}) ;
> A3 = reshape(A2,...)
>
> C = setdiff(A3,B3,'rows')
>
> hth
> Jos

Sorry I made a mistake in the example, that should not have
been 2*B{i}. I am actually working with 3x3 matrices. The 2
should have just been another variable because each matrix
is multiplied by another matrix. I am not using scalars.
thank you for your help!

Tags for this Thread

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.

rssFeed for this Thread

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.

Contact us at files@mathworks.com