dimension problem in matrix multiplication [message #39029] |
Tue, 13 April 2004 11:29 |
Benjamin Hornberger
Messages: 258 Registered: March 2004
|
Senior Member |
|
|
Hi all,
I have a 3-dimensional data array "image" (n_cols, n_rows, n_data) where
cols and rows denote pixels in a 2d-image and data are different data
values for each pixel. Now I would like to perform a matrix
multiplication so that the data vector (n_data elements) for each pixel
is multiplied by a 2-dimensional matrix "c".
If I try result = image # c, I get an "incompatible dimensions" error
since obviously IDL doesn't know that the 3rd dimension in "image" is
the vector I want to multiply by the matrix, and I want that operation
done for every pixel. Playing around with * and 0:n_data-1 subscripts
didn't help so far.
I could imagine a for loop to loop through all pixels, but does anybody
know a way how to avoid the loop? What I did before I found out about
matrix multiplication operators was doing the matrix multiplication
manually:
result = fltarr(n_cols, n_rows, n_data) ;; initialize
for i=0, n_data-1 do begin
for j=0, n_data-1 do begin
result[*, *, i] = result[*, *, i] + c[j, i] * image[*, *, j]
endfor
endfor
But I would like to avoid the for loops and use IDL's matrix
multiplication capabilities.
Any hints?
Thanks,
Benjamin
|
|
|