help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Vector approach to row margin frequencies


From: Judd Storrs
Subject: Re: Vector approach to row margin frequencies
Date: Thu, 25 Jun 2009 03:46:27 -0400

On Thu, Jun 25, 2009 at 12:25 AM, Jaroslav Hajek <address@hidden> wrote:
Surely is, because reshape is an O(1) operation (shares data, only
changes their interpretation). However, reshape/diag/reshape is only
sufficient if you want to scale the leading/trailing dimension of a
N-d array. If you need to scale a dimension in the middle (which is
uncommon, but can happen), you can use permute it to move it to the
end (or beginning, but end is better), or just turn again to repmat or
bsxfun.

Don't underestimate diag() for the middle dimension ;-)

X = ones(256,256,256) ;
f = hamming(256) ;

# merge dimensions 2 and 3, use diag
tic
g = diag(repmat(f,256,1)) ;
reshape(reshape(X,256,65536)*g,256,256,256) ;
toc
Elapsed time is 0.150348 seconds.

# merge dimensions 1 and 2, use diag
tic
g = diag(reshape(repmat(f',1,256),65536,1)) ;
reshape(g*reshape(X,65536,256),256,256,256) ;
toc
Elapsed time is 0.1631 seconds.

# repmat and perform 3D element-by-element multiplication
# most of the performance loss is in creating the full 3D factor
tic
g = repmat(f',[256,1,256]) ;
X.*g ;
toc
Elapsed time is 0.318903 seconds.

# bsxfun
# Performance loss is presumably due to repeated function evaluations
tic
g = reshape(f,1,256,1) ;
bsxfun(@times,X,g) ;
toc
Elapsed time is 2.264 seconds.

Is there any reason why bsxfun style singleton dimension expansion shouldn't be the *default* behavior for the builtin element-wise operators (.*, .+, .-) in octave? bsxfun also seems like the logical extension of the scalar with matrix cases.

Matlab compatibility could be a reason, but I don't think it would break any working Matlab code (unless perhaps some strange code is relying on the error handling).  I think the gain in expressivity and simplicity for octave code could very well be worth it?

Getting back to the original problem about scaling the matrix by row or by column sums if "./" behaved like bsxfun but avoided the repeated function evaluations then the blatently obvious _expression_ of the algorithm would both work and be at least as fast as using diag, while also scaling to higher dimensions sanely:

X ./ sum(X,1)
X ./ sum(X,2)
X ./ sum(X,3)
etc


--judd

reply via email to

[Prev in Thread] Current Thread [Next in Thread]