Displaying empty array as output

19 Ansichten (letzte 30 Tage)
Mughees Asif
Mughees Asif am 19 Mär. 2019
Beantwortet: Guillaume am 19 Mär. 2019
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 Kommentare
Adam
Adam am 19 Mär. 2019
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 am 19 Mär. 2019
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Guillaume
Guillaume am 19 Mär. 2019
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

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by