I'm having trouble with my error message to work.
I have an assignment to calculate the total variance, variance of the diagonal and the variance of the antidiagonal.
And if the input is anything but: 1 matrix with only numbers in it. There should be an error code and the function will be stopped.
My problem here is that when i have the input:
[varAlongDiag, varAlongAntiDiag, varTotal] = e2_2([1,2,3;4,5,6;g])
I get: "Undefined function or variable 'g'."
Do I need to make the matrix accept characters or undefined variables to get my error msg to be delivered instead?
Help much appreciated!
function [varAlongDiag, varAlongAntiDiag, varTotal] = e2_2(A, varargin)
if nargin == 1 && isnumeric(A) && ismatrix(A)
disp ('You only have 1 input')
disp ('Your input is a matrix')
disp ('Your input is only numbers')
B = diag(A);
D = flip(A);
E = diag(D);
varAlongDiag = var(B(:));
varAlongAntiDiag = var(E(:));
varTotal = var(A(:));
else
error('Your input can only include one matrix with numbers only!')
end
5 件のコメント
Adam (view profile)
このコメントへの直接リンク
https://jp.mathworks.com/matlabcentral/answers/484213-adding-characters-to-a-matrix#comment_753909
Star Strider (view profile)
このコメントへの直接リンク
https://jp.mathworks.com/matlabcentral/answers/484213-adding-characters-to-a-matrix#comment_753911
Marcus Niklasson (view profile)
このコメントへの直接リンク
https://jp.mathworks.com/matlabcentral/answers/484213-adding-characters-to-a-matrix#comment_753944
Adam (view profile)
このコメントへの直接リンク
https://jp.mathworks.com/matlabcentral/answers/484213-adding-characters-to-a-matrix#comment_753963
Marcus Niklasson (view profile)
このコメントへの直接リンク
https://jp.mathworks.com/matlabcentral/answers/484213-adding-characters-to-a-matrix#comment_753978
サインイン to comment.