![]() Transposition can be done inline, requiring only a fixed amount of extra memory, independent of array size, or growing very slowly with array size. Permute(., ) %// interchange dims 3 and 4: we have dims Įach of these elementary operations (interchanging two dimensions) is essentially a transpose along a given plane of the multidimensional array, performed repeatedly along all other dimensions. Permute(., ) %// interchange dims 2 and 4: we have dims For example, permute(x, ) is equivalent to this sequence of elementary permute operations (the sequence is not unique): permute(., ) %// interchange dims 1 and 4: we have dims Permuting of dimensions can always be done as a sequence of elementary permute operations, where" elementary" means interchanging only two dimensions. This is only a guess, as I don't really know what permute does under the hood. While this is not exactly the best way to profile memory usage (better use a proper memory profiler, something like Intel Inspector XE), it does show to some degree that permute is indeed not working in-place. You can see how at its peak the function reached twice as much memory usage as when it returned. I also repeated the test under perfmon.exe which showed the same pattern: I then ran the function simply as: %clear aĪnd watched the memory usage It went from 1.8 gigs in use, and rose to 5.2 then quickly down to 3.6 gigs. I only have 8 gigs of RAM on my laptop, so to avoid thrashing I modified your function as: function out = mtest() I also set the "update speed" to "high" to get a finer time resolution. In Windows 10, I opened the "task manager" on the "performance" tab with the "memory" graphs in view. The permute method in fact does create a second copy of the matrix with the data permuted and returns it. )) gives a matrix that contains 0 for entries less than the quantile, and 1 otherwise. Comparing each column with the corresponding quantile (.This means that, in each, column, exactly k entries are less than the column quantile. quantile(X, k/size(X,1)) gives, for each column, a number (quantile) such that a fraction k/size(X,1)) of the entries in that column are less than that number.Use quantile (Statistics Toolbox): X = X. ![]() So, we would have the much needed linear indices like so - > lin_idx =ĥ) Finally, we use these indices to selectively set zeros in X with X(lin_idx) = 0. ![]() Thus, here the expansion of *size(X,1) would take place along the rows and then the elementwise summations with sorted_idx(1:k,:) using would be performed. Please note that this would be done in a vectorized manner. Since we need to do this for every row of sorted_idx(1:k,:), we can use the automatic expansion and summation (with with bsxfun. to put as a generic case *size(X,1), added to sorted_idx(1:k,:). To put it mathematically, we would have, i.e. ![]() ![]() So, following the column-major indexing used in MATLAB, the first column stays as it is, the second column must have the offset of the number of rows in X added, the third column would have 2*number of rows in X added and so on until all columns are covered. Lin_idx = Use those indices to set them in X as zerosĢ) Sorted indices: > = sort(X,1)ģ) Select only the first k indices from each column: > sorted_idx(1:k,:)Ĥ) We need to convert those column-wise indices to linear indices corresponding to the 2D array, X. %// Get linear indices for the first k rows of sorted indices Solution Code: One approach with sort (to get column-wise sorted indices) & then bsxfun (to get linear sorted indices) - %// Get 2D array of column-sorted indices for input X ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |