Sourabh
Sourabh

Reputation: 9

Calling user defined function in MATLAB

I am try to define a function in MATLAB. Then I want to use this function in some other program. The code is as follows:

function E = maxadd(n,m,A,B) 

n = input ('Enter the no. of rows of matrix A or B : '); 

m = input ('Enter the no. of columns of matrix A or B : '); 

A = input ('enter the matrix A, n*m : ');

B = input ('enter the matrix B, n*m : ');

D = -Inf(n,m);

for i=1:n     %addition of matrices

  for j=1:m

      D(i,j)=max (A(i,j), B(i,j));

  end;

end;

E = D ;

end

The program runs ok but, if I run maxadd(2,2,[1 2;3 4],[0 4 ;3 7]) in the command window, it asks me for input again instead of giving me an answer.

Please help me in fixing this issue. My file name is maxadd.m.

Upvotes: 0

Views: 256

Answers (3)

m_power
m_power

Reputation: 3204

You are already passing n, m, A and B as input argument of the maxadd function. You are not supposed to ask for the same values inside the function. If you want the input to be called inside the function, do something similar to this:

function E = maxadd() 

n = input ('Enter the no. of rows of matrix A or B : '); 

m = input ('Enter the no. of columns of matrix A or B : '); 

A = input ('enter the matrix A, n*m : ');

B = input ('enter the matrix B, n*m : ');

D = -Inf(n,m);

for i=1:n     %addition of matrices

  for j=1:m

      D(i,j)=max (A(i,j), B(i,j));

  end;

end;

E = D ;

end

And when you call it:

>> maxadd
Enter the no. of rows of matrix A or B : 2
Enter the no. of columns of matrix A or B : 2
enter the matrix A, n*m : rand(2,2)
enter the matrix B, n*m : rand(2,2)

ans =

    0.8147    0.2785
    0.9058    0.9134

If you don't want to change all calls to the maxadd function, just modify it like that :

function E = maxadd(n, m, A, B) 

D = -Inf(n,m);

for i=1:n     %addition of matrices

  for j=1:m

      D(i,j)=max (A(i,j), B(i,j));

  end;

end;

E = D ;

end

Upvotes: 0

Setsu
Setsu

Reputation: 1228

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:

  1. max already does what you're trying to do. Calling max(A,B) will give you the same output
  2. 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.

Upvotes: 0

johnny
johnny

Reputation: 171

Well for the first part Your code

function E = maxadd(n,m,A,B)

n = input ('Enter the no. of rows of matrix A or B : ');

m = input ('Enter the no. of columns of matrix A or B : ');

A = input ('enter the matrix A, n*m : ');

B = input ('enter the matrix B, n*m : ');

your code has one problem either ask the user for the input or pass the values yourself so you can just use function E = maxadd(n,m,A,B) just pass the values from the command window and thats it no need for this part

n = input ('Enter the no. of rows of matrix A or B : ');

    m = input ('Enter the no. of columns of matrix A or B : ');

    A = input ('enter the matrix A, n*m : ');

    B = input ('enter the matrix B, n*m : ');

Upvotes: 1

Related Questions