for loop - How to vectorize matrix whose index is matrix in MATLAB? -


i have 2 for-loops embedded in code repeated many times. want speed things up:

for = 1:10  j = 1:10   a(i,j) = b(i,j,d(i,j))*c(i,j);   end  end  

here d consist of integers indices b. without dependence on d for-loops replaced elementwise matrix multiplication. problem how evaluate b in elegant way. searched , mathworks-pages , tried linear indexing, produces errors:

d = reshape(d, 100, []); b = reshape(b, 100, []); arrayfun(@(x) b(x,d(x)), 1:100); 

what doing wrong? there way replace 2 for-loops?

you can transform d linear index:

[rows,cols]=ndgrid(1:10,1:10); idx = sub2ind(size(b),rows(:),cols(:),d(:));  = zeros(10,10); %# initialize right size a(:) = b(idx).*c(:); 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -