# How to check if all arrays in a structure are the same size

I have a 1xN-struct `myArr`

(with `N`

a undefined integer) that consists of a set of matrices. These multidimensional arrays can be of different sizes, but I can do certain tasks if they are all equal. How can I make this check?

Example:

``````mystruct =

1x4 struct array with fields:
Scalar
Matrix
```

(adsbygoogle = window.adsbygoogle || []).push({});

```

I wanted to know if 2D matrix fields `Matrix`

that are 2D matrix have the same length in the four structures. Is there a MATLAB function or set of functions to test this easily?

Thank!

+3

source to share

2 answers

Use `arrayfun`

to compare with the `size`

first element:

`````` tf = arrayfun( @(x)  isequal( size(x.Matrix), size(myArr(1).Matrix) ), myArr );
```

(adsbygoogle = window.adsbygoogle || []).push({});

```

`tf`

true if everyone `myArr.Matrix`

has exactly the same `size`

.

Note that it is safer to use `isequal( size(x), size(y) )`

than checking `all( size(x)==size(y) )`

if `x`

u `y`

have a different number of reductions.

+3

source

Alternative way:

``````sz=cellfun(@size, {myArr.Matrix}, 'uni', 0);
allEq = (numel(sz) <= 1) || isequal(sz{:});
```

(adsbygoogle = window.adsbygoogle || []).push({});

```

`myArr.Matrix`

expands to a comma-separated list and `isequal`

can take multiple entries.

+1

source

All Articles