I need to reshape A to dimension 6x200000.
I assume you mean you need to reshape the 1x6x200000 submatrix to 6x200000.
reshaping should always be near instantaneous. All it involves is changing the header of the matrix to store the new size of each dimension. The actual content of the matrix stays untouched and does not need shuffling around.
What would take time however is the slicing, the extracting of the 1x6x200000 matrix out of the 300xXxY full matrix since that involves going over the whole matrix and picking out every 300th value. I would think you may be able to get a small gain of performance if you swapped the order of the dimensions:
newbigmatrix = permute(bigmatrix, [2 3 1]);
for page = size(newbigmatrix, 3):-1:1
submatrix = newbigmatrix(:, :, page);
end
The advantage of that new order is that the elements of each submatrix are already contiguous so the slicing should be faster. It also cuts out the reshape but as said, that shouldn't be what took time in the first place.
Best Answer