Re: general matrix multiplication [message #10994] |
Mon, 23 February 1998 00:00 |
Martin Schultz
Messages: 515 Registered: August 1997
|
Senior Member |
|
|
David Schmidt wrote:
>
> All,
>
> I'm looking for a routine to perform generalized matrix multipication
> over particular indexes within arrays. For example, let A be a
> (10,3,20) array and B be a (20,5,10,30) array. I want to be able to
> multiply and add (i.e. matrix multiply) the elemens within index 1,3 of
> A and 3,1 of B and produce a result of dimension (3,5,10). While this
> can be done simply by using FOR loops, I'm looking for a routine that
> does this efficiently, using built-in IDL routines. Does anyone know of
> such a routine? ...how I could construct such a routine?
>
> Thanks,
>
> David
> --
hmmm - I am not sure I understand how you will get a result with the
dimensions you indicate. Anyway, here some hints that will hopefully
help you a little:
While matrix multiplication is indeed provided by IDL with the
## operator (see online help:
"The ## operator does what is commonly referred to as matrix
multiplication. It computes array elements by multiplying the rows of
the first array by the columns of the second array. The second array
must have the same number of rows as the first array has columns. The
resulting array has the same number of rows as the first array and the
same number of columns as the second array."
I don't think you will have a chance to get around the loops over
3, 5, and 30. Your piece of code should be something like
result = fltarr(10,10,3,5,30)
for i=0,2 do begin
for j=0,4 do begin
for k=0,29 do begin
tmp = A(*,i,*) ## B(*,j,*,k)
result(*,*,i,j,k) = tmp
endfor
endfor
endfor
Regards,
Martin.
------------------------------------------------------------ -------
Dr. Martin Schultz
Department for Earth&Planetary Sciences, Harvard University
186 Pierce Hall, 29 Oxford St., Cambridge, MA-02138, USA
phone: (617)-496-8318
fax : (617)-495-4551
e-mail: mgs@io.harvard.edu
IDL-homepage: http://www-as.harvard.edu/people/staff/mgs/idl/
------------------------------------------------------------ -------
|
|
|