Displaying empty array as output

조회 수: 12 (최근 30일)
Mughees Asif
Mughees Asif 2019년 3월 19일
답변: Guillaume 2019년 3월 19일
function B=IsStable(polynomial)
if AllNonZero(polynomial) == false
H = [];
B = 0;
elseif AllSameSign(polynomial) == false
H = [];
B = 0;
else
H=HurwitzMatrix(polynomial);
pm = length(H);
minor = zeros(1,pm);
for i = 1:pm
minor(i) = det(H(1:i,1:i));
end
if minor > 0
B = 1;
disp(H)
else
B = 0;
disp('[ ]')
end
end
end
The end if statement for the 'minor' displays the H matrix when B = 1 but does not output the disp('[ ]') when B = 0. Any suggestions?
  댓글 수: 2
Adam
Adam 2019년 3월 19일
Is the else part of that of statememnt ever get executed? I assume not, otherwise there is no reason why [ ] would not be displayed.
Adam Danz
Adam Danz 2019년 3월 19일
Those lines of code will only be accessed when all of the following are TRUE
  • AllNonZero(polynomial) == true
  • AllSameSign(polynomial) == true
  • minor <= 0
'B' can be equal to 0 under several different conditions so just because B equals zero doesn't mean that those lines of code were accessed.

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

답변 (1개)

Guillaume
Guillaume 2019년 3월 19일
Hopefully, your real code doesn't use the same indentation as what you've posted above. If it does, I suggest that you let matlab reindent your code (press CTRL+I in the editor).
"The end if statement for the 'minor' displays the H matrix when B = 1"
Your sentence makes no sense. The if statement does not test the value of B. So whether B is 1 or 0 does not matter to what happens.
What the if tests is the values of minor. Note that minor is a vector and therefore minor > 0 is a logical vector. I would suspect that you do not know what happens when you pass a vector to if. What you have written is equivalent to:
if all(minor > 0) %if is true if and only if ALL elements of minor are > 0
B = 1;
disp(H); %so only displayed when ALL of minor is > 0
else %therefore else is executed if just one element of minor is <= 0
B = 0;
disp('[]');
end
It's unclear whether or not that was your intent. If it was, then I recommend that you do use all to make it clear that it is indeed intended.
Note that disp([]) doesn't display anything. If you want to explicitly see [] displayed, then pass it as a char array as I've done above.
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by