Simplifying a "if" statement that checks there is at most one vector

조회 수: 1 (최근 30일)
Hello,
I have got a code looking like this:
if (length(a)>1 && max([length(b) length(c)])>1) || (length(b)>1 && max([length(a) length(c)])>1) || (length(c)>1 && max([length(a) length(b)])>1)
fprintf('This code only allows one non-scalar variable at a time')
return
end
Which stops the scripts if two or more of a, b and c are vector. In my real code there are 8 variables so the "if" is really, really long.
I wanted to know if there is a way to write it another way to make the "if" shorter.
Thank you in advance for answering

채택된 답변

OCDER
OCDER 2019년 1월 15일
a = [1 0 0 1];
b = [2 2 1 2];
c = 4;
if sum(~cellfun(@isscalar, {a b c})) > 1 %You have more than 1 non-scalar
fprintf('This code only allows one vector at a time.\n');
return
end
If your function is uses varargin, you could do this:
%Out = myFunc(a, b, c)
function Out = myFunc(varargin)
Out = [];
if nargin ~= 3
fprintf('Need 3 inputs.\n');
return
elseif sum(~cellfun(@isscalar, varargin)) > 1
fprintf('This code only allows one vector at a time.\n');
return
end
  댓글 수: 1
Marcelin Dierickx
Marcelin Dierickx 2019년 1월 15일
It works, I'll research on the function you use.
Opens me new possibilities.
Thank you :-)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by