Passing one variable out of a matrix

Hi
I have code that calls a function and passes a matrix to it. In the called function, calculations are performed based on only one value at a time from that matrix. How can this be done? At the moment the calculations are using the complete matrix?
thanks

4 Comments

Are you able to give a small example of code for what you mean? It's not easy to understand from just a verbal description.
Yea sure, I did attach the code in a previous question but no one was answering so I thought i'd try verbally.
Code calling function is:
a = fmincon(functmp, [0.04 0.05], [], [], [], [], [0.01 0.01], [0.06 0.06], ineqcon1(dx1));
where dx1 is a 2x1 matrix.
Calculation performed in 'ineqcon1' is:
function [c,ceq] = ineqcon1(dx1)
R = 0.5*(0.05+(0.05+dx1(1)));
You have to define c, ceq in ineqcon1().
Duplicate is at http://www.mathworks.com/matlabcentral/answers/29259-i-m-missing-something

Sign in to comment.

Answers (1)

The code in your previous question on this topic was not using the entire dx1 matrix to calculate with. You must have made an error in your interpretation of the behavior of the function.

12 Comments

well i'm not sure what it is doing then. I had a test case that passed two variables, x1 and x2. This worked however when I pass the matrix, I get 'matrix dimensions must agree'. That was how I interpreted it...
When I hover over R, it is now a matrix whereas before it was a value
Put in a breakpoint on the first line of your function, and examine it step by step. Does dx1(1) return a matrix or a scalar? Does 0.05+dx1(1) return a matrix or a scalar? And so on until you check 0.5*(0.05+(0.05+dx1(1))) and have determined exactly which step converted from a scalar to a matrix. If it is still calculating a scalar, allow the assignment to R and then immediately check the size of R.
dx1(1) returns an equation because in the original code:
xtmp = @(n,a) storextmp(n) + searchtmp(n)*a;
dx1 = @(a) a*searchtmp;
functmp = @(a) 121.126*xtmp(Nvar-1,a)^2*xtmp(Nvar,a)+748.812*xtmp(Nvar-1,a)*xtmp(Nvar,a)+234.323*xtmp(Nvar,a);
a = fmincon(functmp, [0.04 0.05], [], [], [], [], [0.01 0.01], [0.06 0.06], ineqcon1(dx1));
What is searchtmp() ? Is it a function handle or a matrix? In xtmp() you always use searchtmp(n) and you always pass an integer in as "n" to xtmp, so we cannot tell whether searchtmp is a function handle or a matrix. If it is a function handle then dx1 would probably fail as you cannot multiply a function handle by a value.
So probably searchtmp is a matrix, and so dx1(1) is going to return 1 times that matrix, and dx1(2) is going to return 2 times that matrix... either way getting a matrix. And then you are surprised that your R has become a matrix ?
Yea searchtmp() is a matrix.
I thought dx1(1) would return the value of the matrix dx1 at 1,1?
Obviously that isn't so......?
The dx1 you pass in to ineqcon1() is not a matrix: it is the handle to the anonymous function you created using
dx1 = @(a) a*searchtmp;
You have to apply this handle to a value in order to produce a matrix.
It doesn't look like a good idea to me that you are using "a" as the name of a dummy parameter in the anonymous functions, but you are also assigning the result of fmincon() to "a". That's going to confuse people. It probably has already confused the author of the code.
Well yes I am confused which is why I am on this forum asking for help. What I was trying to do was pass the newly calculated values of x (contained in dx1) through to this constraint. The difficulty is that the new values are calculated using the minuses value of a.
I completely acknowledge I am out of my depth with this but everyone has to start somewhere.
Is this possible?
I am working on my employment, and also answering other people's questions, and also trying to find out if what I _think_ you want to do is possible through some hack (it has a bunch of logical problems the way it is now.) It also took me a few minutes to find enough spare change in my drawer to visit the vending machine to take my thoughts off of going home for dinner.
Applications to have your priority increased may be submitted to my Program Manager, if you can manage to find anyone who knows who that _is_.
Sorry. Didn't mean to hassle you. Been spending a lot of time trying to figure this out so my anxiousness is coming through as eagerness.
Appreciate your help.
[...], ineqcon1(@(a) deal(dx1(a),[])) )
Thanks very much for the reply. I am getting an error saying the number of outputs should match the number of inputs.

Sign in to comment.

Categories

Find more on Conway's Game of Life in Help Center and File Exchange

Asked:

on 16 Feb 2012

Edited:

on 15 Oct 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!