You didn't make this clear, but I'm guessing you're trying to write a function that takes optional arguments over the default behavior of asking the user for it. If so, you need to explicitly write the logic in your function, like this:
function E = maxadd(n,m,A,B)
%//prompt user for values if they weren't passed as parameters
if nargin < 1
n = input ('Enter the no. of rows of matrix A or B : ');
end
if nargin < 2
m = input ('Enter the no. of columns of matrix A or B : ');
end
if nargin < 3
A = input ('enter the matrix A, n*m : ');
end
if nargin < 4
B = input ('enter the matrix B, n*m : ');
end
%//your logic here
end
This checks how many input arguments the caller specified, and requests the missing parameters accordingly.
However, we can do better than this.
Typically when we request input from the user, we should always assume that the user can screw up and give us non-sense input. Thus, it is good practice to always check your inputs.
In your case, the first 2 parameters should be numbers, and the last 2 parameters should be matrices, so we check them like this:
function E = maxadd(n,m,A,B)
%//prompt user for values if they weren't passed as parameters
if nargin < 1
n = input ('Enter the no. of rows of matrix A or B : ');
end
if nargin < 2
m = input ('Enter the no. of columns of matrix A or B : ');
end
if nargin < 3
A = input ('enter the matrix A, n*m : ');
end
if nargin < 4
B = input ('enter the matrix B, n*m : ');
end
%//validate input
if ~isnumeric(n)
error('Input parameter n must be numeric');
elseif ~isnumeric(m)
error('Input parameter m must be numeric');
elseif ~ismatrix(A)
error('Input parameter A must be a matrix');
elseif ~ismatrix(B)
error('Input parameter B must be a matrix');
end
%//your logic here
end
But we can improve this further. Notice that n and m are really properties of matrices A and B, and notice that both matrices really need to be the same dimensions for your algorithm to work. Combining this knowledge, we minimize the code like this:
function E = maxadd(A,B)
%//prompt user for values if they weren't passed as parameters
if nargin < 1
A = input ('enter the matrix A, n*m : ');
end
if nargin < 2
B = input ('enter the matrix B, n*m : ');
end
%//validate input
if ~ismatrix(A)
error('Input parameter A must be a matrix');
elseif ~ismatrix(B)
error('Input parameter B must be a matrix');
elseif ~isequal(size(A), size(B))
error('Matrices A and B must have the same dimensions')
end
n = size(A, 1);
m = size(A, 2);
%//your logic here
end
Finally, I would like to point out 2 things:
max already does what you're trying to do. Calling max(A,B) will give you the same output
E isn't necessary at all. If you change the output variable to D and remove the line E = D;, the code will work just as well.