Issues using an efficient positivity constraint
Show older comments
I want to use the following command to obtain only values of 0 or higher for the values in the 600x495 weight matrix p_w:
for j = 1:size(p_w,1)
for i = 1:size(p_w,2)
p_w(j,i) = max(0,p_w(j,i))/nansum(max(0,p_w(j,:)));
end
end
However, when running the optimization it takes a very long time (probably because of the loops), so I have tried to get rid of the loops and came up with the following:
temp = sum(max(0,p_w),2);
p_w_plus = max(0,p_w)./repmat(temp, 1, 495);
However, the optimized values in this case are very different from the function with the loops ,and they are also so extreme that they seem erroneous, so I am probably making a mistake somewhere in transforming the code.
Hopefully someone can tell me what the mistake is or suggest another way to make the first code more efficient.
Thanks in advance,
Martin
Accepted Answer
More Answers (3)
Martin Pott
on 24 Nov 2014
1 vote
To obtain values of 0 or higher, of course you can simply use
p_w = abs(p_w);
or
p_w = max(0, p_w);
Obviously to try to meet another constrain, maybe that the rows contain only positive values that sum to 1? You can achieve this with your second code. The reason why this "optimized" code gives different results than your loop is that you change p_w within the loop, so nansum(max(0, p_w(j,:)) will give different results depending on i.
So far I am not sure what additional constrains your p_w has to meet. Please expand on that.
Martin Pott
on 26 Nov 2014
0 votes
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!