vectorization - how to efficiently/conveniently compute A-inner product (ie. a bilinear form) of many vectors in Matlab? -


i want compute thing v'*m*v in matlab v taken columns of given matrix a, square , potentially large. ie. v=a(:,j)

what convenient , computationally efficient way this? thinking of using bsxfun , possibly reshape not sure how work.

i remember reading similar post long time ago. can't locate it.

the fastest way came loop, not elegant way. maybe else think of better.

function compare(s, v) m = rand(s); = rand(s, v);  %method 1: loop tic r1 = zeros(1, size(a,2)); = 1:size(a,2)     r1(i) = a(:,i)'*m*a(:,i); end dt = toc; disp(['for loop ', num2str(size(a,2)), ' vectors of length ', num2str(size(a,1)), ' ', num2str(dt), ' s.'])  %method 2: cell functions anonymous function tic ap = num2cell(a, 1); r2 = cell2mat(cellfun(@(x) x'*m*x, ap, 'uni', 0)); dt = toc; disp(['cell functions using anonymous function ', num2str(size(a,2)), ' vectors of length ', num2str(size(a,1)), ' ', num2str(dt), ' s.'])  %method 3: vector multiplication tic r3 = diag(a'*m*a); dt = toc; disp(['vector multiplication ', num2str(size(a,2)), ' vectors of length ', num2str(size(a,1)), ' ', num2str(dt), ' s.'])  end 

edit: outputs

>> compare(20, 200)     loop 200 vectors of length 20 0.0016883 s.     cell functions using anonymous function 200 vectors of length 20 0.0079001 s.     vector multiplication 200 vectors of length 20 0.0035036 s. >> compare(20, 400)     loop 400 vectors of length 20 0.0035246 s.     cell functions using anonymous function 400 vectors of length 20 0.010177 s.     vector multiplication 400 vectors of length 20 0.0076295 s. >> compare(20, 800)     loop 800 vectors of length 20 0.0069367 s.     cell functions using anonymous function 800 vectors of length 20 0.022697 s.     vector multiplication 800 vectors of length 20 0.0075474 s. >> compare(20, 1600)     loop 1600 vectors of length 20 0.013802 s.     cell functions using anonymous function 1600 vectors of length 20 0.037844 s.     vector multiplication 1600 vectors of length 20 0.029591 s. >> compare(20, 3200)     loop 3200 vectors of length 20 0.026893 s.     cell functions using anonymous function 3200 vectors of length 20 0.078213 s.     vector multiplication 3200 vectors of length 20 0.084117 s. >> compare(20, 6400)     loop 6400 vectors of length 20 0.053695 s.     cell functions using anonymous function 6400 vectors of length 20 0.15759 s.     vector multiplication 6400 vectors of length 20 0.3524 s. >> compare(40, 1600)     loop 1600 vectors of length 40 0.01514 s.     cell functions using anonymous function 1600 vectors of length 40 0.040556 s.     vector multiplication 1600 vectors of length 40 0.028335 s. >> compare(80, 1600)     loop 1600 vectors of length 80 0.022824 s.     cell functions using anonymous function 1600 vectors of length 80 0.053713 s.     vector multiplication 1600 vectors of length 80 0.047412 s. >> compare(160, 1600)     loop 1600 vectors of length 160 0.045606 s.     cell functions using anonymous function 1600 vectors of length 160 0.096006 s.     vector multiplication 1600 vectors of length 160 0.052472 s. >> compare(320, 1600)     loop 1600 vectors of length 320 0.074407 s.     cell functions using anonymous function 1600 vectors of length 320 0.1386 s.     vector multiplication 1600 vectors of length 320 0.19317 s. >> compare(640, 1600)     loop 1600 vectors of length 640 0.21931 s.     cell functions using anonymous function 1600 vectors of length 640 0.36021 s.     vector multiplication 1600 vectors of length 640 0.24102 s. >> compare(1280, 1600)     loop 1600 vectors of length 1280 1.6893 s.     cell functions using anonymous function 1600 vectors of length 1280 1.8245 s.     vector multiplication 1600 vectors of length 1280 0.57957 s. >> compare(2560, 1600)     loop 1600 vectors of length 2560 6.8812 s.     cell functions using anonymous function 1600 vectors of length 2560 7.0459 s.     vector multiplication 1600 vectors of length 2560 1.2919 s. 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -