Passing one variable out of a matrix
Afficher commentaires plus anciens
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 commentaires
the cyclist
le 16 Fév 2012
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.
Will
le 16 Fév 2012
Sean de Wolski
le 16 Fév 2012
You have to define c, ceq in ineqcon1().
Walter Roberson
le 16 Fév 2012
Duplicate is at http://www.mathworks.com/matlabcentral/answers/29259-i-m-missing-something
Réponses (1)
Walter Roberson
le 16 Fév 2012
0 votes
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 commentaires
Will
le 16 Fév 2012
Walter Roberson
le 16 Fév 2012
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.
Will
le 16 Fév 2012
Walter Roberson
le 16 Fév 2012
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 ?
Will
le 16 Fév 2012
Walter Roberson
le 16 Fév 2012
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.
Will
le 16 Fév 2012
Will
le 16 Fév 2012
Walter Roberson
le 16 Fév 2012
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_.
Will
le 16 Fév 2012
Walter Roberson
le 16 Fév 2012
[...], ineqcon1(@(a) deal(dx1(a),[])) )
Will
le 17 Fév 2012
Catégories
En savoir plus sur Conway's Game of Life dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!