MATLAB Answers

0

Adding characters to a matrix

Marcus Niklasson さんによって質問されました 2019 年 10 月 8 日
最新アクティビティ Jon
さんによって 編集されました 2019 年 10 月 8 日
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); %Extracting the elements of the diagonal of "A"
D = flip(A); %Flipping the original matrix
E = diag(D); %Extracting the elements of the flipped matrix
varAlongDiag = var(B(:));
varAlongAntiDiag = var(E(:));
varTotal = var(A(:));
else
error('Your input can only include one matrix with numbers only!')
end

  5 件のコメント

Marcus Niklasson 2019 年 10 月 8 日
I understand what you mean by it.
But would you say that my function checks that:
There is only 1 input. That the input is a matrix with 2 dimensions. That each element of the matrix is a numeric value. (characters and boolean values are not allowed) If any check fails, the function ends with an error.
Adam
2019 年 10 月 8 日
Yes, it seems to do that to me. Although it seems somewhat contrived to add the varargin argument since you simply want to check that in fact no other arguments are passed in anyway. Usually you would do this simply be declaring your function as:
function [varAlongDiag, varAlongAntiDiag, varTotal] = e2_2(A)
and Matlab would throw its own error if you try to pass any extra arguments into it.
Marcus Niklasson 2019 年 10 月 8 日
Part of the assignment is to have our own error codes stop the program and not have Matlab throw it's own error. Thats why I have the possibility of adding an endless amount of inputs.
Thanks for the help!

サインイン to comment.

タグ

1 件の回答

Jon
回答者: Jon
2019 年 10 月 8 日
編集済み: Jon
2019 年 10 月 8 日
 採用された回答

I think your problem may just be that you need to put single quotes around the g in the function call so use
[varAlongDiag, varAlongAntiDiag, varTotal] = e2_2([1,2,3;4,5,6;'g'])
When you make the call without the quotes MATLAB thinks that there should be a variable named g which is defined in the workspace and doesn't find it. Using 'g' lets MATAB know that this is a character.
You may still have some other issues with the array you are defining, maybe you mean
[varAlongDiag, varAlongAntiDiag, varTotal] = e2_2([1,2,3;4,5,6],'g')

  1 件のコメント

Walter Roberson
2019 年 10 月 8 日
[1,2,3;4,5,6;'g'] would be incompatible dimensions, as the first two rows have three columns but the third row is only a scalar.
[51,52,53;54,55,56;'gab'] is an example of something that would not have incompatible dimensions. Due to the rules about combining numeric and character in the same array, it would all become character, giving the result
3×3 char array
'345'
'678'
'gab'

サインイン to comment.



Translated by