Prev: matlab function
Next: spm_select
From: Peter on 5 Mar 2010 11:37 Hi, I have a 5xN matrix, A, which I want to multiply by a Nx1 vector, b, such that the first element of b multiplies all of the first column elements of A. Another way to say this is that I want to perform a tensor product called the Tracy-Singh or Khatri-Rao product. (The motivation for doing this is to save on having to write an explicit loop to do this product, which I assume would take longer since N is not so small. The time becomes a factor as this operation is being done in a Monte-Carlo simulation, i.e. b is a random vector, and so has to be repeated many times.) Anyhow, I don't see any MatLab function that supports this operation. Does anyone knw if such a direct operation exists or have any suggestions? TIA, Peter
From: Matt J on 5 Mar 2010 11:57 Peter <pi2oversix(a)gmail.com> wrote in message <a5655f2c-8fe0-4437-a6cf-fd79495c7269(a)g7g2000yqe.googlegroups.com>... > Anyhow, I don't see any MatLab function that supports this operation. > Does anyone knw if such a direct operation exists or have any > suggestions? ============== There is bsxfun(), but a for-loop over the rows (not the N columns!!) would probably be the fastest way since you have such a small number fo rows result=A; for ii=1:5 result(ii,:)=result(ii,:).*b(:).' end
From: Doug Schwarz on 5 Mar 2010 12:14 Peter wrote: > Hi, > > I have a 5xN matrix, A, which I want to multiply by a Nx1 vector, b, > such that the first element of b multiplies all of the first column > elements of A. Another way to say this is that I want to perform a > tensor product called the Tracy-Singh or Khatri-Rao product. > (The motivation for doing this is to save on having to write an > explicit loop to do this product, which I assume would take longer > since N is not so small. The time becomes a factor as this operation > is being done in a Monte-Carlo simulation, i.e. b is a random vector, > and so has to be repeated many times.) > Anyhow, I don't see any MatLab function that supports this operation. > Does anyone knw if such a direct operation exists or have any > suggestions? > > TIA, Peter The bsxfun syntax is bsxfun(@times,A,b.') -- Doug Schwarz dmschwarz&ieee,org Make obvious changes to get real email address.
From: Peter on 5 Mar 2010 12:43 On Mar 5, 11:57 am, "Matt J " <mattjacREM...(a)THISieee.spam> wrote: > Peter <pi2over...(a)gmail.com> wrote in message <a5655f2c-8fe0-4437-a6cf-fd79495c7...(a)g7g2000yqe.googlegroups.com>... > > Anyhow, I don't see any MatLab function that supports this operation. > > Does anyone knw if such a direct operation exists or have any > > suggestions? > > ============== > > There is bsxfun(), but a for-loop over the rows (not the N columns!!) would probably be the fastest way since you have such a small number fo rows > > result=A; > for ii=1:5 > > result(ii,:)=result(ii,:).*b(:).' > > end Yes, that's very nice and makes sense. Thanks! Peter
|
Pages: 1 Prev: matlab function Next: spm_select |