Code covered by the BSD License  

Highlights from

5.0 | 1 rating Rate this file 2 Downloads (last 30 days) File Size: 1.55 KB File ID: #36636 Version: 1.2




10 May 2012 (Updated )

Determine if all inputs are the same size in all dimensions.

| Watch this File

File Information

samesize: takes any number of arrays of any type and returns true if
the dimensions of all the objects are the same


result = samesize(A, B, ...)


A, B etc are arrays or matrices of any type. samesize returns true if
they are all the same size in every dimension, and have the same number
of dimensions, or false otherwise

samesize returns true for zero arguments, or a single argument on the
basis that the input will definately be the same size as itself in these


astruct = struct('a',{1,2,3}); % creates a (1 x 3) struct array
anarray = [1,2,3];
acellarray = {2+6j, [1,2,3,4,5,6,7,8], 'a string'};
samesize(astruct, anarray, acellarray)

 ans =

samesize(astruct, anarray', acellarray)

 ans =

See also: size, ndims

Tested in R2008a and R2011a


This file inspired Regexprepfile, Strrepfile, and Refactor Fcn Name.

MATLAB release MATLAB 7.6 (R2008a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (4)
25 Aug 2013 Jan Simon

Jan Simon (view profile)

Another hint: ISEQUAL replies a scalar TRUE or FALSE, so there is no need for ALL().

Comment only
14 May 2012 Richard Crozier

@Jan, oh, and thanks for the useful review.

Comment only
14 May 2012 Richard Crozier


Actually, I see now that isequal will return false in this case, so maybe I will just get rid of the first test!

Comment only
12 May 2012 Jan Simon

Jan Simon (view profile)

"any(diff(numdims))" is faster than "numel(unique(numdims)) > 1", but I'd omit the test for equal number of dimensions completely.

The function works as expected, the task is useful, it has a H1-line and a help text with an example and a "See also" line.

14 May 2012 1.1

used any(diff(numdims)) suggestion from commenter

14 May 2012 1.2

removed first test for number of dimensions.

Contact us