What is a simple way to check if a collection of vectors all have the same number of elements
Show older comments
I am look for a nice simple way to check if a collection of vectors all have the same number of elements. For example this might be used to validate some inputs to a function, or otherwise check compatibility before performing other operations.
Here's my current approach, which works, but I wonder if someone has simpler way of doing the same thing
a = rand(3,1);
b = rand(3,1);
c = rand(4,1);
d = rand(3,1);
vecs = {a,b,c,d};
n = cellfun(@numel,vecs);
if any(n(1)~=n)
error('all vectors must be same length')
end
2 Comments
Torsten
on 22 Sep 2023
Why not renaming a,b,c and d to vecs{1},vecs{2},vecs{3} and vecs{4} ? Should make things easier ...
Bruno Luong
on 22 Sep 2023
Edited: Bruno Luong
on 22 Sep 2023
I'm half serious but:
The simplest way? IMO just don't do any check, just rely on MATLAB, if any operation on vecs with incompatible sizes will imply error, just let MATLAB throw an error instead of
error('all vectors must be same length')
Accepted Answer
More Answers (1)
a = rand(3,1);
b = rand(3,1);
c = rand(4,1);
d = rand(3,1);
vecs = {a,b,c,d};
n = cellfun(@numel,vecs,'uniform',0);
if ~isequal(n{:})
error('all vectors must be same length')
end
6 Comments
Dyuman Joshi
on 22 Sep 2023
Using 'length' instead of @numel might have some benefit (in certain circumstances).
Stephen23
on 22 Sep 2023
@Dyuman Joshi: can you give an example? In general the advice given on this forum is to avoid LENGTH due to its changing dimensions without warning... a behavior that is certainly the cause of a few threads on this forum.
Dyuman Joshi
on 22 Sep 2023
Edited: Dyuman Joshi
on 22 Sep 2023
The idea behind my comment is that, that the use of some functions being slower than using the function names as strings for cellfun, such as for length, isempty etc.
I am aware of the consensus of the community for length and I personally advocate for the use of numel over length as well.
But in some obscure case, it might have some benefit.
Edit - @Bruno has implemented another such function - 'prodofsize' in their answer, and you can see that there is significant performance difference.
Walter Roberson
on 22 Sep 2023
Edited: Walter Roberson
on 22 Sep 2023
L = randi([0 10], 1, 50);
vecs = arrayfun(@(L) rand(1,L), L, 'uniform', 0);
timeit( @() cellfun(@numel,vecs,'uniform',0), 1)
timeit( @() num2cell(cellfun('length',vecs)), 1)
There is an oddity that when you pass a character vector for the function name, cellfun does not accept any options such as uniformoutput
The difference in timings above is not large on an absolute scale, so one should wonder whether the difference is within measurement error -- but it was pretty consistent here on MATLAB answers over several experiments
Jon
on 2 Oct 2023
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!