![]() ![]() This does slow it down a bit but not enough to consider (the lines will cross at 600圆00 instead of 400x400). I have also tested the following which will convert the cell into a matrix so that the return values of the two functions are the same: function T = split_by_mat2cell (A, m, n) Note that the Y axis is in log scale, so the difference between the two functions for a 100x100 matrix is 0.02 seconds while for a 10000x10000 matrix is 100 seconds. Performance wise, using the nested permute will only be faster for smaller matrices where big changes in relative performance are actually very small changes in time. Xlabel ("Length of matrix side (all squares)") T = mat2cell (A, repmat (m, l(1), 1), repmat (n, l (2), 1)) Īsizes = Īs = arrayfun Asizes, "UniformOutput", false) T = permute (reshape (permute (reshape (A, size (A, 1), n, ), ), n, m, ), ) function T = split_by_reshape_permute (A, m, n) The code was run in Octave (version 3.9.1) with JIT disabled. ![]() ![]() when this problem arises, it is preferable (in my experience) to have the data in a cell since later on one will often want to put the original back together Īnyway, I have compared them both with the following script.the cell can be easily converted into a matrix like the other (I timed this, see further down).It is true that they don't return the exact same thing but: With some many upvotes for the answer that makes use nested calls to permute, I thought of timing it and comparing to the other answer that makes use of mat2cell. Example used here is MatLab in-built image 'cameraman.tif' Here is an example of a Gray Scale image (2D). Results:Īdvantage of this method is, it works good on 2D images as well. Once the process is done, the dimensions are restored as it was, by permuting back. To achieve this, additional permuting was done to swap 3rd and 4th dimensions. The basic idea is to make the 3rd Dimension unaffected while reshaping so that the image isn't distorted. N = size(A,2)/nCol %// Sub-matrix column size (Should be an integer) M = size(A,1)/nRow %// Sub-matrix row size (Should be an integer) Donda's answer I would like to expand his answer to 3D matrices so that this technique could be used in cropping True Color images (3D) A = imread('peppers.png') %// size(384x512x3) Even though the question is basically for 2D matrices, inspired by A. ![]()
0 Comments
Leave a Reply. |