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