From owner-help-octave at bevo dot che dot wisc dot edu Thu Nov 14 16:42:07 1996 Subject: Re: Efficient multiplication by a diagonal matrix From: SANDS at VSDEC dot NL dot NUWC dot NAVY dot MIL To: mstorti at galileo dot unl dot edu dot ar Cc: help-octave at bevo dot che dot wisc dot edu Date: Thu, 14 Nov 1996 17:41:46 -0400 (EDT) Mario Storti writes: >I found myself repeatedly with the following problem. Given a matrix >A(n,m) and a vector v(n), I have to multiply each row A(j,:) by >v(j). This is equivalent to compute: > >B = diag(v) * A (1) > >Now, for large n, (1) is very inefficient ... Right, try using "Tony's Trick", it's been around for years. Assuming v is a column vector (if not do v=v(:);) try B=v(:,ones(length(v),1)) .* A; This is a kind-of MATLAB specific thing but I think it works in octave o.k. Scott Sands