Generalizing an IF and FPRINTF expression
이전 댓글 표시
Hello,
I got a part of code looking like this:
A = ... % can be a scalar or a vector
B = ... % can be a scalar or a vector if A is scalar
if any(B-A) <= 0 && length(B-A) == length(B)
fprintf('B > %g or A < %g',A,B(1,find(B-A<=0,1)))
return
elseif any(B-A) <= 0 && length(B-A) == length(A)
fprintf('B > %g or A < %g',A(1,find(B-A<=0,1)),B)
return
end
A and B can't both be vectors at the same time. They can both be scalars at the same time
Is there a way to have only one FPRINTF ?
It would need the code not to acces out of (1,1) for the scalar variable.
It's to have less lines in the final code. I would use it elsewhere too. It may be a bit perfectionism but would be usefull.
Thank you in advance for answering.
EDIT: actually the question is "How to return 1 if the index exceeds matrix dimensions" ?
댓글 수: 2
Guillaume
2019년 1월 15일
I suspect that your
any(B-A) <= 0
is supposed to be
any(B-A <= 0)
Otherwise, it doesn't make much sense.
I don't really understand what you're trying to achieve but in any case, code clarity should trump succinctness. What your code is mising is a comment that explains what it is doing. Personally, I would have written your current code as:
if any(B - A <= 0)
if numel(B) > 1
fprintf('B > %g or A < %g',A,B(1,find(B-A<=0,1)))
elseif numel(A) > 1
fprintf('B > %g or A < %g',A(1,find(B-A<=0,1)),B)
end
end
Even longer but clearer, and avoids doing the same any test twice.
Are the return actually necessary?
Marcelin Dierickx
2019년 1월 16일
편집: Marcelin Dierickx
2019년 1월 16일
채택된 답변
추가 답변 (1개)
Guillaume
2019년 1월 16일
The code checks if B is greater than A so that the solid will sink in the atmosphere, it's to ensure that I didn't give wrong values to A and B.
Then, in my opinion, you're taking the wrong approach. I would make the code errors if the inputs do not satisfy the preconditions required by it. The best way to do that is, at the beginning of the function, to use validateattributes and/or assert
validateattributes(A, {'numeric'}, {'vector', 'finite'}); %scalar pass the vector check
validateattributes(B, {'numeric'}, {'vector', 'finite'});
assert(isscalar(A) || isscalar(B), 'One of A or B must be scalar');
assert(all(B > A), 'B must be greater than A');
This is much clearer than your accepted answer. It self-documents the preconditions of your code and the user gets a descriptive error message telling them what the problem is.
카테고리
도움말 센터 및 File Exchange에서 Scripts에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!