1/31/2024 0 Comments Randomly permute array in matlabPlease note that arrayfun isn't a vectorized solution as most certainly it uses loops behind-the-scenes and seems like mat2cell is using for loops inside its source code, so please do keep all these issues in mind. ![]() This seems to perform much better than with mat2cell in terms of performance. Hi, I have a vector which has n by 1 entries. Now, another approach if you would like to preserve the cell format would be with arrayfun, assuming each cell of M to be a 4D numeric array - M = num_array(:,:,:,:,x),1:N,'Uniform',0) ![]() Some quick tests show that mat2cell would prove to be the bottleneck, so if you don't mind indexing into the intermediate numeric array variable num_array and use it's last dimension for an equivalent indexing into M, then this approach could be useful. M = squeeze(mat2cell(num_array,size_num_arr2c,ones(1,num_cells))) %// thus obtained numeric array from its first to the second last dimension Now, another approach if you would like to preserve the cell format would be with arrayfun, assuming each cell of M to be a 4D numeric array - M arrayfun ( (x) numarray (:,:,:,:,x),1:N,'Uniform',0) This seems to perform much better than with mat2cell in terms of performance. %// Save the numeric array as a cell array with each block from Num_array = permute(reshape(num_array,size_num_arr),perm_dim) You may be asked to use it to generate a random value within a specific range, generate a random valid index in an array or list, or make a conditional. %// for indexing data from different cells ends up as the final dimension %// Reshape and permute the numeric array such that the index to be used %// Store data from input M into a vertically concatenated numeric array %// Dimensions array for permuting with the numeric array holding all data %// input cell array with the second dimension representing the index of Parameters: xint or arraylike If x is an integer, randomly permute np.arange (x). ![]() Note New code should use the permutation method of a Generator instance instead please see the Quick Start. If x is a multi-dimensional array, it is only shuffled along its first index. %// Get size of the numeric array that will hold all of the data from the Randomly permute a sequence, or return a permuted range. This seems to work - num_cells = numel(M) %// Number of cells in input cell array
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |