File Exchange

image thumbnail

RANDBLOCK

version 1.1.0.0 (2.82 KB) by Jos (10584)
scramble matrices using blocks (v2.3, mar 2011)

10 Downloads

Updated 24 Mar 2011

View Version History

View License

RANDBLOCK - randomize blocks of a matrix

R = RANDBLOCK(M,S) randomizes the matrix M by dividing M into non-overlapping blocks of the size specified by S, and shuffling these blocks. M can be a N-D matrix.

The number of elements in S should match the number of dimensions of M, or S can be a scalar specifying a S-by-S-by-S-by ... block size. S should contain positive integers. The size of M in any dimension should be an integer number of times the specified size of the block in that dimension (e.g., if size(M,1) equals 6, S(dim) can be 1,2,3, or 6).

[R,I,J] = RANDBLOCK(...) also returns indices I and J, so that R equals A(I)and R(J) equals A.

M can be a numerical or cell array.

Examples:
% Shuffle blocks of 3 elements of a 15-element vector
M = 1:15 ;
randblock(M,3)

% Scramble a 2D matrix
M = reshape(1:24,4,[]) ;
randblock(A,[3 2]) % randomize the position of the four 3-by-2 blocks
randblock(A,2) % randomize the position of the six 2-by-2 blocks

% Scramble a 3D volume
M = reshape(1:64,[4 8 2]) ;
randblock(A,[2 4 2]) % randomize the position of the four
% 2-by-4-by-2 sub-volumes
% Scramble a cell matrix
M = {'1','a','bb','1c' ; '2a',[3 4 5],'2c','2dd'} ;
randblock(M,[2 2]) % randomize the position of the four 2-by-2 blocks

% Scramble a RGB image Z, and retrieve the original using the
% indices (see the SCREENSHOT)
Z = peaks(200) ; Z = cat(3,Z,flipud(Z), -(circshift(Z.',[100,100]))) ;
Z = (Z - min(Z(:))) ; Z = Z./ max(Z(:)) ;
[Z2,I,J] = randblock(Z,[25,25,size(Z,3)]) ; % Scramble 25-by-25 blocks
subplot(2,2,1) ; image(Z) ; title('Original image') ;
subplot(2,2,2) ; image(Z2) ; title('25x25 scrambled image') ;
subplot(2,2,3) ; image(Z2(J)) ; title('Z2(J) = original image') ;
subplot(2,2,4) ; image(Z(I)) ; title('Z(I) = scrambled image') ;
set (gcf,'Name','[Z2,I,J] = randblock(Z,[25,25,size(Z,3)])') ;

Version 2.3 (mar 2011)

Cite As

Jos (10584) (2021). RANDBLOCK (https://www.mathworks.com/matlabcentral/fileexchange/17981-randblock), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (5)

Isha Dhingra

Hi Jos, I am using this code to create control stimuli for a fMRI study - please let me know the best/most appropriate way to credit or cite your work. Thanks - isha

Jos (10584)

What do you mean by unshuffle? Per the second exmaple, Z2(J) might give you that ...
Why rate it with 3 stars? Can you be more specific?

Fawad Masood

Fawad Masood

Salam i need unshuffle of this code please

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!