From: J on
How would one vectorize the code below? It seems like it should be a fairly simple thing to do...


% The matrix 'A' is a 3D data cube of size N x N x 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(di,:) = [ind(di),ind(dj),ind(dk),A(di,dj,dk)];
end
end
end
From: J on
EDIT:

The line:

L(di,:) = [ind(di),ind(dj),ind(dk),A(di,dj,dk)];

should have read:

L((N^2)*(di-1)+N*(dj-1)+dk,:) = [ind(di),ind(dj),ind(dk),A(di,dj,dk)];


----------------
> How would one vectorize the code below? It seems like it should be a fairly simple thing to do...
>
>
> % The matrix 'A' is a 3D data cube of size N x N x 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(di,:) = [ind(di),ind(dj),ind(dk),A(di,dj,dk)];
> end
> end
> end
From: us on
"J " <philNOSPAMsonj(a)spammenot.hotmailDcom> wrote in message <hp2g0h$ksh$1(a)fred.mathworks.com>...
> How would one vectorize the code below? It seems like it should be a fairly simple thing to do...
>
>
> % The matrix 'A' is a 3D data cube of size N x N x 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(di,:) = [ind(di),ind(dj),ind(dk),A(di,dj,dk)];
> end
> end
> end

what is A and N and DELT(?)...
don't just copy/paste - make sure CSSMers are able to copy/paste your stuff and run it in their command window...
in summary, produce a piece of code that can be used without second guessing...

us
From: Joao Henriques on
"Vectorize This Please"? :P

This shoud be pretty close to what you want, if not, it should at least point you in the right direction.

ind = -250*delt:delt:250*delt;

[I,J,K] = meshgrid(ind, ind, ind);

L = [I(:), J(:), K(:), A(:)];
From: Matt Fig on
X = repmat({1:N},3,1);
[X Y Z] = ndgrid(X{:});
L2 = [ind([Z(:) Y(:) X(:)]) reshape(permute(A,[3 2 1]),[],1)];