# Math Help - Matlab matrix in function

1. ## Matlab matrix in function

Hey ppl,

I have a matlab function which calls on another function like so:
-------------------------------------------------------------------------
Function 1:

function E = solve_it(m,t,i)

%where m is a 2 column matrix with 1 or more rows(amount is of the users choosing)

r=m(:,1);
s=m(:,2);
A=r./s;
invE=A+1/t+1/i;
E=1/invE;

-------------------------------------------------------------------------
Function 2:

function B = solve_them(m,f,g,t,i,t1,t2)

E=solve_it(m,t,i);
B=E*f*g*(t1-t2);

-------------------------------------------------------------------------
For the following inputs into B i get the correct answers:

>> B = solve_them([2 4],4,5,2,3,7,2)

B =

75

>> B = solve_them([3 5],4,5,2,3,7,2)

B =

69.7674

------------------------------------------------------------------------
Yet putting in both as 1 matrix i get 1 correct and 1 always at 0

>> B = solve_them([2 4;3 5],4,5,2,3,7,2)

B =

0 69.7674

When the output i would desire is

75
69.7674
-------------------------------------------------------------------------
Can anyone identify what im doing wrong, im guessing its something to do with me using the m(:,1) and m(:,2) selections.

2. You need to evaluate each row of variables in "m" if you intend to use the function the way you have shown. Try something like this:

Code:
function E = solve_it(m,t,i)

%where m is a 2 column matrix with 1 or more rows(amount is of the users choosing)
for j = 1:size(m,1);
r=m(j,1);
s=m(j,2);
A=r./s;
invE=A+1/t+1/i;
E(j,1)=1/invE;
end
Regards Elbarto

3. Originally Posted by deragon999
Hey ppl,

I have a matlab function which calls on another function like so:
-------------------------------------------------------------------------
Function 1:
Code:

function E = solve_it(m,t,i)

%where m is a 2 column matrix with 1 or more rows(amount is of the users choosing)

r=m(:,1);
s=m(:,2);
A=r./s;
invE=A+1/t+1/i;
E=1/invE;

-------------------------------------------------------------------------
Function 2:

function B = solve_them(m,f,g,t,i,t1,t2)

E=solve_it(m,t,i);
B=E*f*g*(t1-t2);
Set a break point on the line "r=m(:,1);" then single step through to the line "E=1/invE;" checking what the result is at each stage. Is what you observe what you expect?

In particular what do you expect 1/invE to be when invE is a vector?

CB