## array dimensions equivalence check?

### Walter Roberson (view profile)

on 4 Jan 2012
Latest activity Edited by Walter Roberson

### Walter Roberson (view profile)

on 21 Sep 2016 at 20:01

### David Young (view profile)

You would think I would know this by now, but I never had reason to program it before.

Is there a good idiom for efficiently checking that two arrays are the same size or are row and column vectors of the same length?

For example, this kind of check would be used to find out whether two arrays are assignment-compatible, or to find out if two arrays are compatible for plot() purposes.

The code I have come up with feels overly complicated:

```szA = size(A);
szB = size(B);
```
```if isequal(szA,szB) || ...
(sum(szA==1) == 1 && sum(szB==1) == 1 && ...
numel(szA)==2 && numel(szB)==2) && ...
prod(szA) == prod(szB))
```

Which is to say, the sizes can be identical, or they can both be 2 dimensional with exactly one of the dimensions of each being 1, and the number of elements the same between the two.

This kind of check must be programmed all over the place. Perhaps there is a utility routine that I overlooked?

Paulo Abelha

### Paulo Abelha (view profile)

on 21 Sep 2016 at 12:09

Hello Walter,

https://uk.mathworks.com/matlabcentral/fileexchange/59168-checknumericarraysize--a--dim-sizes--

Best regards,

## Products

### David Young (view profile)

on 4 Jan 2012

One alternative:

```isequal(size(A), size(B)) || (isvector(A) && isvector(B) && numel(A) == numel(B))
```

Walter Roberson

### Walter Roberson (view profile)

on 4 Jan 2012

Thanks, that looks better than what I had.

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi