필터 지우기
필터 지우기

What is a simple way to check if a collection of vectors all have the same number of elements

조회 수: 13 (최근 30일)
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
all vectors must be same length
  댓글 수: 2
Torsten
Torsten 2023년 9월 22일
Why not renaming a,b,c and d to vecs{1},vecs{2},vecs{3} and vecs{4} ? Should make things easier ...
Bruno Luong
Bruno Luong 2023년 9월 22일
편집: Bruno Luong 2023년 9월 22일
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')

댓글을 달려면 로그인하십시오.

채택된 답변

Bruno Luong
Bruno Luong 2023년 9월 22일
편집: Bruno Luong 2023년 9월 22일
if any(n(1)~=n)
The above will crash if your cell is empty.
IMO
if any(diff(n))
is a more robust test. Thus you can do the test in a single statement
if any(diff(cellfun(@numel,vecs)))
% ...
end
And last but not least, if performance matters, use this
if any(diff(cellfun('prodofsize',vecs)))
% ...
end
instead of @numel.
Here is why
vecs = arrayfun(@(x) zeros(randi(10),1), 1:1e6, 'unif', 0);
timeit(@()cellfun(@numel, vecs),1)
ans = 0.7885
timeit(@()cellfun('prodofsize', vecs),1)
ans = 0.0098
  댓글 수: 1
Jon
Jon 2023년 9월 22일
Thanks, very much, I will look into this too. Away for a week, no MATLAB, but will review again when I get back

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 9월 22일
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
all vectors must be same length
  댓글 수: 6
Jon
Jon 2023년 9월 22일
편집: Jon 2023년 9월 22일
@Walter Roberson That's neat, I didn't realize that isequal could take a variable number of input arguments, e.g. n{:},
I also thought about, using a try catch block something like below. I think my original approach (incorporating Walter's suggested modifications), seems cleaner than the try-catch. Maybe this is just a subjective issue, but wondered whether anyone has any thoughts on comparing the two ways and which approach is "better".
a = rand(3,1);
b = rand(3,1);
c = rand(4,1);
d = rand(3,1);
try
M = horzcat(a(:),b(:),c(:),d(:))
catch
error('all vectors must be same length')
end
all vectors must be same length
Jon
Jon 2023년 10월 2일
Also, a very good answer, and good discussion, but I could only accept one. Thanks for all of your help.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by