Andi
Andi

Reputation: 4855

Vectorize shuffle for blocks of matrix rows

I have a simple 5x15 matrix called mat_orig. I would like to divide this matrix into non-overlapping blocks, whereby each block is of length 3. That is equivalent to put three non-overlapping rows into one block (i.e. there are 5 blocks of size 3x5).

I would then like to shuffle mat_orig and generate a new matrix. I am using randi to randomly draw five blocks.

I succeeded in finishing my task using the code below. However, I am wondering if I can get rid of the for-loop? Can I apply vectorization?

mat_orig = reshape(1:75, 5, 15)';

block_length = 3;

num_blocks = size(mat_orig, 1) / block_length;

rand_blocks = randi(num_blocks, num_blocks, 1);

mat_shuffled = nan(size(mat_orig));

for r = 0 : num_blocks - 1
    start_row_orig = r * block_length + 1;
        end_row_orig = r * block_length + block_length;
    start_row_random_blocks = ...
        rand_blocks(r + 1) * block_length - block_length + 1;
    end_row_random_blocks = ...
        rand_blocks(r + 1) * block_length;

    mat_shuffled(start_row_orig:end_row_orig, :) = ...
        mat_orig(start_row_random_blocks:end_row_random_blocks, :);
end

Upvotes: 4

Views: 106

Answers (1)

Wolfie
Wolfie

Reputation: 30047

You can do this with implicit expansion to create the blocks' indices, see the code comments for details.

mat_orig = reshape(1:75, 5, 15)';
block_length = 3;
num_blocks = size(mat_orig,1) / block_length;

% Get the shuffle order (can repeat blocks)
shuffIdx = randi( num_blocks, num_blocks, 1 ).';
% Expand these indices to fill the blocks
% This uses implicit expansion to create a matrix, and 
% will only work in R2016b or newer.
% For older versions, use 'bsxfun'
shuffIdx = block_length * shuffIdx - (block_length-1:-1:0).';
% Create the shuffled output
mat_shuffled = mat_orig( shuffIdx(:), : );

Upvotes: 3

Related Questions