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 matrices

Subject: comparing matrices

From: sumeet pushpam

Date: 10 May, 2010 05:28:05

Message: 1 of 6

i have 'M', a 10X11 matrix having different elements.
and now i have 'B' 1X11 matrix which i want to compare with each of M's 10 rows and find out which row it is closest to??

thanks

Subject: comparing matrices

From: Roger Stafford

Date: 10 May, 2010 06:13:03

Message: 2 of 6

"sumeet pushpam" <sahil1806@gmail.com> wrote in message <hs85h5$c2j$1@fred.mathworks.com>...
> i have 'M', a 10X11 matrix having different elements.
> and now i have 'B' 1X11 matrix which i want to compare with each of M's 10 rows and find out which row it is closest to??
>
> thanks

  You need to define what you mean by "closest" between 11-element rows. There are a number of different definitions of "distance" between vectors.

Roger Stafford

Subject: comparing matrices

From: sumeet pushpam

Date: 10 May, 2010 06:24:05

Message: 3 of 6

by closest i mean nearest. lets take an example..

say A= [ 1 2 3 ; 10 11 12; 20 21 22]
a 3x3 matrix and
B= [12 10 8], then i should get second row as the result of my question.sort of element by element comparison in each row.

thanks.

Subject: comparing matrices

From: Oleg Komarov

Date: 10 May, 2010 07:42:05

Message: 4 of 6

"sumeet pushpam" <sahil1806@gmail.com> wrote in message <hs88q5$9ji$1@fred.mathworks.com>...
> by closest i mean nearest. lets take an example..
>
> say A= [ 1 2 3 ; 10 11 12; 20 21 22]
> a 3x3 matrix and
> B= [12 10 8], then i should get second row as the result of my question.sort of element by element comparison in each row.
>
> thanks.

% Find the absolue distance in an elementiwise row by row comparison:
Clos = sum(abs(bsxfun(@minus, A,B)),2);
Clos =
         24.00
          7.00
         33.00

[val, row] = min(Clos);
val =
          7.00
row =
          2.00

Is this what you're looking for?

Oleg

Subject: comparing matrices

From: sumeet pushpam

Date: 10 May, 2010 08:32:05

Message: 5 of 6

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <hs8dcd$1n9$1@fred.mathworks.com>...
> "sumeet pushpam" <sahil1806@gmail.com> wrote in message <hs88q5$9ji$1@fred.mathworks.com>...
> > by closest i mean nearest. lets take an example..
> >
> > say A= [ 1 2 3 ; 10 11 12; 20 21 22]
> > a 3x3 matrix and
> > B= [12 10 8], then i should get second row as the result of my question.sort of element by element comparison in each row.
> >
> > thanks.
>
> % Find the absolue distance in an elementiwise row by row comparison:
> Clos = sum(abs(bsxfun(@minus, A,B)),2);
> Clos =
> 24.00
> 7.00
> 33.00
>
> [val, row] = min(Clos);
> val =
> 7.00
> row =
> 2.00
>
> Is this what you're looking for?
>
> Oleg



yes !!.. thanks mate...

Subject: comparing matrices

From: Steven Lord

Date: 10 May, 2010 13:49:34

Message: 6 of 6


"sumeet pushpam" <sahil1806@gmail.com> wrote in message
news:hs88q5$9ji$1@fred.mathworks.com...
> by closest i mean nearest. lets take an example..
>
> say A= [ 1 2 3 ; 10 11 12; 20 21 22]
> a 3x3 matrix and B= [12 10 8], then i should get second row as the result
> of my question.sort of element by element comparison in each row.

Okay, so again, what do you mean by "closest"?

b = [1 1 1];
a = [1 100 10; 2 2 2; 1.1 1.1 2.5]

If "closest" means "the minimum of the difference between b and the chosen
row of a is smallest among rows of a" then you want row 1.
If "closest" means "the maximum of the difference between b and the chosen
row of a is smallest among rows of a" then you want row 2.
If "closest" means "the sum of squares of the difference between b and the
chosen row of a is smallest among rows of a" then you want row 3.

So again, what SPECIFICALLY do you mean by "closest"?

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

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