Code covered by the BSD License  

Highlights from
RANDBLOCK

Be the first to rate this file! 18 Downloads (last 30 days) File Size: 2.82 KB File ID: #17981
image thumbnail

RANDBLOCK

by Jos (10584)

 

12 Dec 2007 (Updated 24 Mar 2011)

scramble matrices using blocks (v2.3, mar 2011)

| Watch this File

File Information
Description

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)

MATLAB release MATLAB 6.5 (R13)
Other requirements should work for R13 and up (most recently tested in 2010b).
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Updates
13 Dec 2007

repaired minor m-lint warnings in v2.1

24 Mar 2011

modified example in the help

Tag Activity for this File
Tag Applied By Date/Time
matrices Jos (10584) 22 Oct 2008 09:39:06
random Jos (10584) 22 Oct 2008 15:04:16
shuffle Jos (10584) 22 Oct 2008 15:04:21
volume Jos (10584) 22 Oct 2008 15:04:26
image Jos (10584) 22 Oct 2008 15:04:32
scramble Jos (10584) 22 Oct 2008 15:04:39
randomize Jos (10584) 22 Oct 2008 15:04:49
matrices Jos (10584) 22 Oct 2008 15:05:20
matrix Jos (10584) 22 Oct 2008 15:05:24
scramble it ieci 24 Feb 2009 12:12:45
change Jos (10584) 24 Mar 2011 16:14:27
blocks Jos (10584) 24 Mar 2011 16:14:27
rand Jos (10584) 24 Mar 2011 16:14:27
rearrange Jos (10584) 24 Mar 2011 16:14:27
order Jos (10584) 24 Mar 2011 16:14:27
permute Jos (10584) 24 Mar 2011 16:14:27

Contact us at files@mathworks.com