Prev: Problems with loading file
Next: Missing a couple of basic (and very necessary) features in containers.Map
From: J on 1 Apr 2010 13:10 Okay, so vectorize *this*: ------------- N = 10; delt = 5/10; A = rand(N,N,N); L = zeros(N^3,4); ind = -250*delt:delt:250*delt; for di=1:N for dj=1:N for dk=1:N L((N^2)*(di-1)+N*(dj-1)+dk,:) = [ind(di),ind(dj),ind(dk),A(di,dj,dk)]; end end end ------------- I can see why an experimentalist would want exact values and code that runs, so I do apologize. In theory though, one should be able to vectorize that regardless of the exact values--i.e., the vectorized version is independent of whether N=10 or N=500... a similar argument holds for the exact value of delt or the values stored in the 3D matrix A.
From: Matt Fig on 1 Apr 2010 13:16 "Matt Fig" <spamanon(a)yahoo.com> wrote in message <hp2jau$eas$1(a)fred.mathworks.com>... > X = repmat({1:N},3,1); > [X Y Z] = ndgrid(X{:}); > L2 = [ind([Z(:) Y(:) X(:)]) reshape(permute(A,[3 2 1]),[],1)]; By the way, this FOR loop setup is faster than your original, and faster than the above vectorization: L = zeros(N^3,4); cnt = 0; for di=1:N for dj=1:N for dk=1:N cnt = cnt + 1; L(cnt,1) = ind(di); L(cnt,2) = ind(dj); L(cnt,3) = ind(dk); L(cnt,4) = A(di,dj,dk); end end end Depending on N, you might want to check which is best for your needs.
From: J on 1 Apr 2010 13:25 Thanks Joao & Matt! "Matt Fig" <spamanon(a)yahoo.com> wrote in message <hp2jau$eas$1(a)fred.mathworks.com>... > X = repmat({1:N},3,1); > [X Y Z] = ndgrid(X{:}); > L2 = [ind([Z(:) Y(:) X(:)]) reshape(permute(A,[3 2 1]),[],1)];
From: Jan Simon on 1 Apr 2010 15:31 Dear Phil! > In theory though, one should be able to vectorize that regardless of the exact values--i.e., the vectorized version is independent of whether N=10 or N=500... If you ask for a vectorized version, CSSMers assume, that you want a fast version. Then it matters if N = 10 or N = 1E9 or UINT16(1023)! In addition even experienced programmers make some errors. If one can copy and paste the original code and can compare the results, such bugs can be found (sometimes). Of course the CSSMers can define constants by their own, but it is much faster, if the OP do as much work as possible. Kind regards, Jan
First
|
Prev
|
Pages: 1 2 Prev: Problems with loading file Next: Missing a couple of basic (and very necessary) features in containers.Map |