Got Questions? Get Answers.
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:
compare elements of two diferrent rows

Subject: compare elements of two diferrent rows

From: Konst

Date: 26 Nov, 2010 17:06:03

Message: 1 of 7

I have a table
1.1406 0 0 1.5691 0.4286
1.3203 1.5691 0.2627 0 0
2.1636 2.7719 0.6083 0 0
how can i do this:
  s=size(table)
    for i=1:s(1)
      if table(i,4)==table(i+1,2)
          if table(i,2)==0
             table(i+1,:)=[] %delete the i+1 row
          end
      end
   end
without getting the error :index exceeds matrix dimensions?
That is, how can I compare two different rows of the same matrix and delete the one of them if they have the same value in the second column?
Does anyone know?Perhaps it's obvious but I'm at a loss!Please help...

Subject: compare elements of two diferrent rows

From: Oleg Komarov

Date: 26 Nov, 2010 17:38:03

Message: 2 of 7

"Konst " <konstance1@hotmail.com> wrote in message <icopdr$mkp$1@fred.mathworks.com>...
> I have a table
> 1.1406 0 0 1.5691 0.4286
> 1.3203 1.5691 0.2627 0 0
> 2.1636 2.7719 0.6083 0 0
> how can i do this:
> s=size(table)
> for i=1:s(1)
> if table(i,4)==table(i+1,2)
> if table(i,2)==0
> table(i+1,:)=[] %delete the i+1 row
> end
> end
> end
> without getting the error :index exceeds matrix dimensions?
> That is, how can I compare two different rows of the same matrix and delete the one of them if they have the same value in the second column?
> Does anyone know?Perhaps it's obvious but I'm at a loss!Please help...

In your example you have no rows with the same value in the second column.
Also, in your loop you're not doing what you described with words.

Come up with a consistent description of what you're trying to achieve and/or use ismember and logical indexing.

Oleg

Subject: compare elements of two diferrent rows

From: Konst

Date: 26 Nov, 2010 17:56:03

Message: 3 of 7

The cells (1,4) and (2,2) have the same values.I want to compare these two cells and if they have the same value delete the second row.That's why I put the if table(i,4)==table(i+1,2) statement.
See the view original format for the matrix..
Thanks for your time!

Subject: compare elements of two diferrent rows

From: Miroslav Balda

Date: 26 Nov, 2010 20:04:04

Message: 4 of 7

"Konst " <konstance1@hotmail.com> wrote in message <icosbj$7p$1@fred.mathworks.com>...
> The cells (1,4) and (2,2) have the same values.I want to compare these two cells and if they have the same value delete the second row.That's why I put the if table(i,4)==table(i+1,2) statement.
> See the view original format for the matrix..
> Thanks for your time!

Hi Konstance,
One of possible solutions is as it follows:

table=...
    [1.1406 0 0 1.5691 0.4286
     1.3203 1.5691 0.2627 0 0
     2.1636 2.7719 0.6083 0 0]
m = size(table,1);
j = zeros(m,1);
for i=1:m-1
    if (table(i,4)==table(i+1,2)) && table(i,2)==0
        j(i+1) = 1; % delete the j(i)th row
    end
end
%table = table(j==0,:)
table = table(~j,:)

Mira

Subject: compare elements of two diferrent rows

From: Oleg Komarov

Date: 26 Nov, 2010 20:17:03

Message: 5 of 7

"Konst " <konstance1@hotmail.com> wrote in message <icosbj$7p$1@fred.mathworks.com>...
> The cells (1,4) and (2,2) have the same values.I want to compare these two cells and if they have the same value delete the second row.That's why I put the if table(i,4)==table(i+1,2) statement.
> See the view original format for the matrix..
> Thanks for your time!

So after you delete the sedon row, what you want to do?

Oleg

Subject: compare elements of two diferrent rows

From: Konst

Date: 26 Nov, 2010 20:38:03

Message: 6 of 7

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <icp4jv$e0e$1@fred.mathworks.com>...
> "Konst " <konstance1@hotmail.com> wrote in message <icosbj$7p$1@fred.mathworks.com>...
> > The cells (1,4) and (2,2) have the same values.I want to compare these two cells and if they have the same value delete the second row.That's why I put the if table(i,4)==table(i+1,2) statement.
> > See the view original format for the matrix..
> > Thanks for your time!
>
> So after you delete the sedon row, what you want to do?
>
> Oleg
I just sort the table and the programm ends there
I'll try Mira's way and let you know.Thank you both!

Subject: compare elements of two diferrent rows

From: ImageAnalyst

Date: 26 Nov, 2010 21:17:49

Message: 7 of 7

You're doing floating point comparisons. That means that unless your
numbers are hard coded in, and not arrived at from some kind of
arithmetic or electrical operation, the code may not work. You will
need to allow for some small tolerance when comparing them, like

if (number1 - number2) < 0.00001

rather than
if number1 == number2

See the FAQ:
http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

Tags for this Thread

No tags are associated with 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