generalization of matrix product?

  • Follow


Hi,
Suppose we have two matrices
R = [r1
       r2]
and
C = [c1 c2],
where r1 and r2 are row vectors and c1 and c2 are column vectors of equal length.
The standard matrix product R*C can be viewed as 
[dot(r1, c1) dot(r1, c2)
 dot(r2, c1) dot(r2, c2)]
clearly this extends to any number of rows in R and columns in C.

Is there a neat way in matlab to perform an operation between two matrices similar to the above where the dot() function is replaced by a user defined function between two vectors?
0
Reply A 4/27/2010 11:26:05 PM

"A " <aaarbk@geemail.com> wrote in message <hr7rqd$9vj$1@fred.mathworks.com>...
> Hi,
> Suppose we have two matrices
> R = [r1
>        r2]
> and
> C = [c1 c2],
> where r1 and r2 are row vectors and c1 and c2 are column vectors of equal length.
> The standard matrix product R*C can be viewed as 
> [dot(r1, c1) dot(r1, c2)
>  dot(r2, c1) dot(r2, c2)]
> clearly this extends to any number of rows in R and columns in C.
> 
> Is there a neat way in matlab to perform an operation between two matrices similar to the above where the dot() function is replaced by a user defined function between two vectors?

help bsxfun

For example...

fun = @(x,y) x.^2 + y.^2;
x = [1;2;3];
y = [2 3 5 7];
bsxfun(fun,x,y)
ans =
     5    10    26    50
     8    13    29    53
   13    18    34    58
 
John
0
Reply John 4/28/2010 12:28:03 AM


"John D'Errico" <woodchips@rochester.rr.com> wrote in message <hr7vej$hq$1@fred.mathworks.com>...
> "A " <aaarbk@geemail.com> wrote in message <hr7rqd$9vj$1@fred.mathworks.com>...
> > Hi,
> > Suppose we have two matrices
> > R = [r1
> >        r2]
> > and
> > C = [c1 c2],
> > where r1 and r2 are row vectors and c1 and c2 are column vectors of equal length.
> > The standard matrix product R*C can be viewed as 
> > [dot(r1, c1) dot(r1, c2)
> >  dot(r2, c1) dot(r2, c2)]
> > clearly this extends to any number of rows in R and columns in C.
> > 
> > Is there a neat way in matlab to perform an operation between two matrices similar to the above where the dot() function is replaced by a user defined function between two vectors?
> 
> help bsxfun
> 
> For example...
> 
> fun = @(x,y) x.^2 + y.^2;
> x = [1;2;3];
> y = [2 3 5 7];
> bsxfun(fun,x,y)
> ans =
>      5    10    26    50
>      8    13    29    53
>    13    18    34    58
>  
> John

Hi John,
Thank you for your response. I am familiar with bsxfun and its application to the sort of example you have shown. However, in my example there is no "singleton" dimension; the matrices R and C satisfy the same size requirements as those for a general matrix product (although it might be possible in a more general setting to do away with even this requirement).  
So for example:
A is a matrix of size n*d, storing a collection of n d-dimensional points, and define
euc = @(x,y) norm(x-y)
so
whatIwant(euc, A,A')
would return the Euclidean distance matrix between every pair of points in A.
0
Reply A 4/28/2010 5:01:05 AM

2 Replies
102 Views

(page loaded in 0.332 seconds)


Reply: