matlab - vectorized block assign with overlopping values -



matlab - vectorized block assign with overlopping values -

so ran bug today

a_test(dest,:)=a_test(source,:)+a_test(dest,:); a_test(:,dest)=a_test(:,source)+a_test(:,dest);

if dest non-unique, fails (which makes sense). quick prepare loop on dest

for (k=1:numel(dest)) a(dest(k),:)=a(source(k),:)+a(dest(k),:); a(:,dest(k))=a(:,source(k))+a(:,dest(k)); end

and matlab bad @ such loops. how 1 vectorize call?

with following, show how rows. columns, it's similar approach different code, i'll explain why.

to summarize, have matrix a, n rows , p columns. have list of integers in range [1,n], src, , idem dst. i'm assuming both have same size, , might contain more n elements (so there repetitions in both potentially).

grouping srcs dsts, it's clear operation you're talking equivalent linear recombination of rows. equivalent pre-multiplication n x n matrix in element (i,j) = k means "the recombination corresponding destination row contains row j multiplicity k".

this next code does:

function = madscience( a, src, dst ) n = size(a,1); m = eye(n); ix = sub2ind( [n,n], dst, src ); [ux,~,mlt] = unique( ix ); nux = length(ux); mlt = accumarray( mlt(:), 1, [nux,1] ); m(ux) = m(ux) + mlt'; = m*a; end

note 1: 2 codes give in post not equivalent; need 2 separate for loops create them equivalent.

note 2: same operation on columns equivalent post multiplication matrix in element (i,j) = k means "the recombination corresponding column j contains column multiplicity k".

note 3: if square, both operations can performed same matrix m (m*a) * m' (the parenthesis important).

matlab vectorization

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -