From: Jim on
Hello guys,

I am coding a rather large application and am faced by another stumbling block and need your help and expertise.

Firstly, as follows is the function:

%======================================
function Return = Return_answer (x, X)
Return = -(x * X(1,1) + X(3,1)) / X(2,1);
%======================================

Now in my application I am faced with the multiple values in the 'x' and 'X' variables, hence I'll need to do some looping.

So, I will begin explaining my issue:

VAL = [ 2 5 6 11;
8 7 9 15 ]

xmin = VAL(1)
ymin = VAL(2)
xmax = VAL(3)
ymax = VAL(4)

Now, my first problem here is that with the matrix 'VAL' I want to distinguish 'xmin' , 'ymin' , 'xmax' and 'ymin' for each row in VAL.

I am given in my application 'X' to be defined as:

X = [34 87;
59 79;
45 22]

This is where my second problem comes in. In the function 'Return' , X is supposed to be in the form X = [ 34; 59 ;45 ] and X = [87 ; 79; 22].

So, finally , to get my final result, I use the function in code as follows:

%==============================================
X = [ 34; 59 ;45 ];
VAL = [ 2 5 6 11];

xmin = VAL(1)
ymin = VAL(2)
xmax = VAL(3)
ymax = VAL(4)

Return = Return_answer(xmax, X)
%==============================================

The return answer is

Return = -4.22033898305085

I am new to Matlab and coding in general and not too sure on looping, So basically, I would like to get a return answer using looping.

With the other set of input values into the function as follows:

X = [ 87; 79 ; 22 ];
VAL = [ 8 7 9 15];

The return answer is

Return = -10.1898734177215

To conclude, I want in the end to get a final output answer of the form:

Return =
-4.22033898305085
-10.1898734177215

If any of you can help, please do, I'm in strife.

jimmy
From: ImageAnalyst on
jimmy:
You say "In the function 'Return' , X is supposed to be in the form X
= [ 34; 59 ;45 ] and X = [87 ; 79; 22]. " First of all, you don't have
a function called Return - you have one called Return_answer.
Secondly, return is a reserved word in MATLAB, and even though it's
case sensitive, it's best not to use reserved words as variables. For
example you wouldn't use these variable names For, Do, While,
Function, Plot, etc. because the lower case versions of those are
reserved words. Likewise it's dangerous to have both x and X for
variable names.

Now, when you say "X is supposed to be in the form X = [ 34; 59 ;45 ]
and X = [87 ; 79; 22]" I'm not sure if X is supposed to be 2D or 1D.
It sounds like the calling program has it as D and you say that (for
some reason) that you want to pass in a 1D X yet in your code inside
the function return_answer you clearly must have X be a 2D array.
You've got ME confused. In your ideal situation, would you pass in a
1D X or a 2D X???

Next, you calculate ymin and ymax, and xmin. What is the use of those
variables? They don't seem to be used at all.

Finally, when I test your code:
function test()
X = [34 87;...
59 79;...
45 22]
VAL = [2 5 6 11;...
8 7 9 15 ]

xmin = VAL(1)
ymin = VAL(2)
xmax = VAL(3)
ymax = VAL(4)

returnValue1 = Return_answer(xmax, X)

%======================================
function returnValue = Return_answer(x, X)
returnValue = -(x * X(1,1) + X(3,1)) / X(2,1);
%======================================

It reports this:
X =
34 87
59 79
45 22

VAL =
2 5 6 11
8 7 9 15

xmin =
2
ymin =
8
xmax =
5
ymax =
7
returnValue1 =
-3.6441

and -3.6441 is not one of your desired output values.
Please try again to explain better.
From: Jim on
ImageAnalyst,

thanks ,I will re-explain and just below is written function.

%==================================
function myfunc = myfunc_answer (a, X)
myfunc = -(a * X(1,1) + X(3,1)) / X(2,1);
%==================================

1) I am given VAL and X in the formats as seen below as outputs from another program:

VAL = [ 2 5 6 11;
8 7 9 15 ]

X = [34 87;
59 79;
45 22]

2) Now there are 2 rows as we know in VAL: ' 2 5 6 11' and ' 8 7 9 15'
They are already organised in the following format : ' amin bmin amax bmax'.

3) With respect to the inputs into the function "(a,X)" , I am suppose to basically use either 2 and 8 as "a" ,i.e. both are "amin" as can be seen. And since both 2 and 8 needs to be accounted for , I said I would like to use some type of looping so the function can be repeated two times for both 2 and 8 as inputs.

I also have to use the same function replacing 'a' with the "bmins" ,that is , with 5 and 7 as inputs, then similarly with the "amax" and "bmax".

It is already clear 'X' is the other input and entered in a columnwise manner.

Using "amax" as input for "a" as an example. Basically, if you enter in Matlab as follows you should get the answers I wanted:

%===============================
a = 6 , % that is, using amax as input for example
X = [ 34; 59 ;45 ];
myfunc = myfunc_answer(a, X)
%===============================

myfunc =

-4.22033898305085

The next step would be to use a = 9,that is the other "amax" value in the matrix "VAL" and the other X column ,that is, X = [ 87; 79 ; 22 ], which would output as follows:

myfunc =

-10.1898734177215

I would basically like to loop this entire procedure 2 times so my final output for the 'amax' set of vales would be:

myfunc =

-4.22033898305085
-10.1898734177215

,and also in a similar manner for when 'amin', 'bmin' and 'bmax' are used as inputs.

Hope I am clearer now.
Appreciate the help my friend

jimmy
From: ImageAnalyst on
jimmy:
It's still confusing. I've got this now - see if it helps:

function test()
X = [34 87;...
59 79;...
45 22]
VAL = [2 5 6 11;...
8 7 9 15 ]

xmin = VAL(1)
ymin = VAL(2)
xmax = VAL(3)
ymax = VAL(4)

% Execute with the two amins for the first argument.
% Execute with the first column of X as the second argument.
% First do for the first columns.
returnValue1 = myfunc_answer(VAL(:,1), X(:,1))

% Now loop through doing all columns of VAL and X
for vColumn = 1 : size(VAL, 2)
for xColumn = 1 : size(X, 2)
fprintf(1, 'Using X column %d, VAL column %d\n',...
xColumn, vColumn);
returnValue1 = myfunc_answer(VAL(:, vColumn), X(:, xColumn))
end
end
%======================================
function returnValue = myfunc_answer(maxOrMins, X)
returnValue = -(maxOrMins .* X(1) + X(3)) / X(2);
%======================================

But I still don't know what you're trying to do overall. Are you
trying to get some kind of average, interpolated value, clipped value,
or what???? Why don't you just describe in words what the overall
result is supposed to be when you pass in VAL and X. I'm sure we can
do it passing in the whole VAL and X, not just a column at a time, but
I don't know what you want to do.


From: Jim on
Indeed my friend, I am trying do a clipping of sorts. In your code you parse in the following manner:

Using X column 1, VAL column 1
Using X column 2, VAL column 1
Using X column 1, VAL column 2
Using X column 2, VAL column 2
..
..
..
..and so on

If it is possible, how can I get it to parse like the following? :

Using X column 1, VAL(1,1)
Using X column 2, VAL(2,1)
Using X column 1, VAL(1,2)
Using X column 2, VAL(2,2)
Using X column 1, VAL(1,3)
Using X column 2, VAL(2,3)
Using X column 1, VAL(1,4)
Using X column 2, VAL(2,4)
..
..
..and so on

If i can get this my problem will be solved.

Cheers
jimmy