Code covered by the BSD License  

Highlights from
repToSize

4.0

4.0 | 1 rating Rate this file 2 Downloads (last 30 days) File Size: 2.29 KB File ID: #29106

repToSize

by Jason Kaeding

 

21 Oct 2010

Replicate and tile an array so that it meets a specified final size.

| Watch this File

File Information
Description

repToSize is an adaptation of repmat which requires only the final array size as the input. This removes the need to calculate the number of repetitions as well as handling for cases where a a fractional number of repetitions is required.

If the final
  size is not a multiple of the original array's size, the appropriate partition
  of the array will be used to fill the remaining values.
 
  In pictoral form for the 2D case:
  
  If A = [m x n array], then
  B = repToSize(A,x,y) produces
                    | A A .. A A2 |
                    | A A .. A A2 |
                    | .. .. .. .. .. |
                    | A A .. A A2 |
                    | A1 A1 .. A1 A3 |
  where x >= m, y >= n, and
                    A1 = A(1:rem(x,m),:)
                    A2 = A(:,1:rem(y,n))
                    A3 = A(1:rem(x,m),1:rem(y,n))
 
  If, for example, x < m and y < n, then B will simply be equal to A3.
 
 
  Example:
  A = reshape([1:3*4*5],[3 4 5]); % a 3x4x5 array of values from 1 to 60
  B = repToSize(A,[13 19 3]) % create a 13 x 19 x 3 array, replicating values
  B = repToSize(A,13,19,3) % alternate syntax
  
  In this example, B(:,1,:) will contain repeated copies of [1;2;3] with a 1
  as the final entry (13 is not a multiple of 3). B(1,:,:) will
  contain repeated copies of [1,4,7,10], ending in 7 as the last entry in the
  row (19 is not a multiple of 4).

MATLAB release MATLAB 7.11 (2010b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
22 Oct 2010 Sean de

4 Stars for now.

I like this function and think it would be useful. My only issue with it is the way it handles a replication when the new size is not evenly divisible by the old size. Such as:
repToSize(magic(3),9,4)
repTosize(magic(3),2,2)

There should be a warning when this happens saying that the replication wasn't evenly divisible, and even better if it said what it truncated. Something along the lines of:
repTosize(magic(3),1,2)
warning
column 3 and rows 2-3 were not repeated evenly.

The warning could be optionally turned off.

13 Feb 2011 Jason Kaeding

Appreciate the comment. I guess the idea of this function was that I just needed to fill a matrix with repeated values, not caring about any truncation.

One situation in particular is with color order. Suppose you use 10 different colors on your graph. Now you want 47 curves. To build the new color matrix, I know it doesn't fit, and I just want it to fill it up in order.

In other words, I tend to see a warning as suggesting unanticipated behavior, such as complex data on a plot. Here, the "intended" behavior is to truncate the dimensions. Perhaps an optional output argument would be best where you could get some information back?

[newMatrix,???] = repToSize(...)

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
repmat Jason Kaeding 22 Oct 2010 10:23:22
circshift Jason Kaeding 22 Oct 2010 10:23:22
matrix manipulation Jason Kaeding 22 Oct 2010 10:23:22

Contact us at files@mathworks.com