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 Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by