I have a large vector containing integers (length: 230 400, int8) which I rearrange into a 3-dimensional matrix (rows: 240, cols: 320, depth: 3). When this has been done the new matrix contains doubles instead of ints (according to Matlab Workspace).
The whole operation takes 0.3 seconds, which is to long for my purposes.
Is Matlab converting the ints in the array to doubles before putting them in the matrix? Can this be avoided to开发者_C百科 speed it up?
Some code:
tic;
A=zeros(240,320,3);
%A is matrix, B is vector.
for i=1:240
for j=1:320
A(i,j,:)=B(1+(j-1)*3+(i-1)*320*3:3+(j-1)*3+(i-1)*320*3);
end
end
toc;
Thanks!
Just use reshape on B. This is faster and will preserve the class of B.
A = reshape(B,[3,320,240]); %# makes a 3-by-320-by-240 array and distributes elements of B
A = permute(A,[3 2 1]); %# turns A into a 240-by-320-by-3 array
Try initializing your matrix with
A=zeros(240, 320, 3, 'int8')
精彩评论