MATLAB: How to assign a large multi-demensional matrix fast by index and another matrix

efficienthugelargematrixslow

here is my situation
Nx=400; Ny=500; Nz=300; Nm=50; Np=10;
mtx = zeros(Nx,Ny,Nz,Nm,Np);
for idx = 1:length(Ny_list)
mtx(:,Ny_list(idx),Nz_list(idx),Nm_list(idx),Np_list(idx))=linedata(idx,:);%linedata is the acquired data, size(linedata) is 100,000×400
%Ny_list,Nz_list,Nm_list,Np_list are 100,000×1 vectors indicating the indexes
end
And is ultra-slow
How can i speed the matrix assignment?
Thanks!

Best Answer

Indices = sub2ind([Ny,Nz,Nm,Np],Ny_list,Nz_list,Nm_list,Np_list);
mtx = zeros(Nx,Ny,Nz,Nm,Np);
mtx(:,Indices)=linedata(1:numel(Indices),:).';