Skip to content

Instantly share code, notes, and snippets.

@jnkather
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnkather/aa95e29d901cfd24f3dc to your computer and use it in GitHub Desktop.
Save jnkather/aa95e29d901cfd24f3dc to your computer and use it in GitHub Desktop.
Create a 1D or 2D stairstep array in Matlab
% (c) Jakob Nikolas Kather 2015
% contact: http://www.kather.me
% aim is to create a 1D-stairstep array, i.e. transform
% 1 2 3 4 to
% 1 1 1 2 2 2 3 3 3 4 4 4
matrix = 1:4
newSize = 3
step_matrix = reshape(repmat(matrix,newSize,1),1,[])
% aim is to create a 2D-stairstep array, i.e. transform
% 1 2
% 3 4
% to
% 1 1 2 2
% 1 1 2 2
% 3 3 4 4
% 3 3 4 4
matrix = reshape(1:4,[],2)'
oldSize = 2
newSize = 4
[xold yold] = meshgrid(linspace(0,1,oldSize),linspace(0,1,oldSize));
[xnew ynew] = meshgrid(linspace(0,1,newSize),linspace(0,1,newSize));
step_matrix = interp2(xold,yold,matrix,xnew,ynew,'nearest')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment