is it possible to use a lookup table for optimization?
14 次查看(过去 30 天)
显示 更早的评论
I am doing an optimization and need to include a look up table as constraint. A variable is another's lookup table value. Is there a way I can do that?
0 个评论
采纳的回答
Walter Roberson
2017-1-8
Beyond what the others posted: the additional bit of information you need is https://www.mathworks.com/help/matlab/math/parameterizing-functions.html
7 个评论
Walter Roberson
2017-1-10
You have
check_YPlot1 = @(param)cdf(makedist('weibull','a',MU(param),'b',param1),peakdata-param2);
Here you have bound the word "param" as a local variable for the purpose of anonymous function. Inside the @(param) body, "param" no longer means [param1,param2]. So there is no connection between the @(param) and the param1 and param2 in the body of the anonymous function. If you are expecting a vector of two parameters then you should index them:
check_YPlot1 = @(param)cdf(makedist('weibull','a',MU(param),'b',param(1)),peakdata-param(2));
This would create an anonymous function that expected a vector of two values as input and would call cdf with expressions based on the parameters, getting back a vector of results. That sounds like something you might plausibly want to do.
Now look for a moment at mc5
X.r_check=size(peakdata);
mc5=linspace(1,X.r_check(1),X.r_check(1));
That definition of mc5 is the same as
mc5 = 1 : X.r_check(1);
and establishes mc5 as an array of row indices, of length X.r_check(1). So mc5(1:X.r_check(1)) is going to be the same as mc5(1:end) which is going to be just 1 : X.r_check(1) -- consecutive integers. And there are going to be as many of them as your peakdata is tall.
Now look at your
f3=@(param)(((sum((log(1./(1-check_YPlot1(mc5(1:X.r_check(1))))).*log(log(1./(1-check_YPlot1(mc5(1:X.r_check(1)))))))))/(sum(log(1./(1-check_YPlot1(mc5(1:X.r_check(1)))))))-((1/X.r_check(1))*(sum(log(log(1./(1-check_YPlot1(mc5(1:X.r_check(1))))))))))/param1 +(1/X.r_check(1)*(sum(log(check_XPlot1(mc5(1:X.r_check(1)))-param2))))-(sum(log((check_XPlot1(mc5(1:X.r_check(1)))-param2).*((check_XPlot1(mc5(1:X.r_check(1)))-param2).^param1))))/(sum((check_XPlot1(mc5(1:X.r_check(1)))-param2).^param1)))^2+(1/X.r_check(1)*(sum((1./(check_XPlot1(mc5(1:X.r_check(1)))-param2))))*(sum((check_XPlot1(mc5(1:X.r_check(1)))-param2).^param1))/(sum((check_XPlot1(mc5(1:X.r_check(1)))-param2).^(param1-1)))-(((1/X.r_check(1))*(sum(log(1./(1-check_YPlot1(mc5(1:X.r_check(1))))))))*(sum((log(1./(1-check_YPlot1(mc5(1:X.r_check(1)))))).^(-1/param1)))/(sum((log(1./(1-check_YPlot1(mc5(1:X.r_check(1)))))).^((param1-1)/param1)))))^2;
You invoke that check_YPlot1 function multiple times, passing in to it mc5(1:X.r_check(1)). We just established above that mc5 expression is the same as 1:X.r_check(1) itself, the vector of integers. So you are calling check_YPlot1 with 1:X.r_check(1) as the vector of arguments, which will not typically be of length 2 as your peakdata will usually have more than 2 rows.
You are passing that vector of integers into check_YPlot1 that expects a vector of exactly two parameters, corresponding to param1 and param2. If that is deliberate, then you are effectively calling check_YPlot1([1,2]) a whole bunch of times. I can't see a good reason for that.
AGGGH
You have
check_YPlot1 = zeros(X.r_check(1),1);
check_YPlot1 = X.rank_check./(X.r_check(1)+1);
mu=((1/(X.r_check(1)*((1/X.r_check(1))*(sum(log(1./(1-check_YPlot1(mc5(1:X.r_check(1)))))))))*(sum((check_XPlot1(mc5(1:X.r_check(1)))-param2).^param1)))^1/param1);
so you first define check_YPlot1 as a vector, then you overwrite it with what looks likely to be another vector, then you define mu in terms of that vector and subscripts, and then you define a function check_YPlot1. And then you define f3 in terms of a whole bunch of calls to that function, with arguments that look exactly like what you used when you were indexing a vector! This is getting really confusing!
I do see now that your f3 = @(param) uses param1 and param2, and perhaps you were thinking of those when you wrote your function check_YPlot1 ???
You need to define more clearly:
- what parameters are to be passed in to the function check_YPlot1 ?
- How does mc5(1:X.r_check(1)) get you those parameters?
- Are you trying to define your mu and check_YPlot1 recursively ??
Have another look at your f3 function. You have a bunch of calls to
check_YPlot1(mc5(1:X.r_check(1))
For the purpose of analysis, assign that value to a variable such as YP1 everywhere in a copy of the f3 definition.
YP1 = check_YPlot1(mc5(1:X.r_check(1));
and replace in the code. You will then see that you have a bunch of (1-YP1) expressions and that you have no other check_YPlot1 left. So simplify again, say calling it OneYP
OneYP = 1 - YP1;
You will now find that all OneYP occur in the form log(1./OneYP) so you can reduce that again,
logrOneYP = log(1./OneYP);
Your code is now much shorter and you finally do not always use that variable the same way. So, now back-substitute and turn it into a function:
Logr = @(param) log(1./(1-check_YPlot1(param)));
and write calls to that function in place of the longer expression in f3.
Now you still have references to check_XPlot1(mc5(1:X.r_check(1)) in the function and you could simplify those. The earlier analysis above suggests that can be replaced by just check_XPlot1 (the entire vector).
I will leave off there due to confusion about what you are really trying to calculate.
更多回答(2 个)
Image Analyst
2017-1-8
Sure. If you have a finite set of input values but a ton of numbers to compute it on, then it makes sense to compute it just once for every possible input value and build a look up table. Then just apply the value from lookup table to every value. It will be much faster.
For example if you have some complicated formula
outputGrayLevel = SomeComplicatedFormula(inputGrayLevel);
and you need to apply that 20 million times for a 20 megapixel image, then it makes sense to just do it 256 times for every possible gray level (of an 8 but integer image) and then use
outputImage = intlut(inputImage, lut);
Using the built-in intlut() function will be much, much faster than computing that complicated formula 20 million times.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Special Values 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!