Issues using an efficient positivity constraint
3 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Thorsten
2014 年 11 月 24 日
編集済み: Thorsten
2014 年 11 月 24 日
I see. Than you can use your second code or (only one max, probably faster):
temp = max(0,p_w);
p_w_plus = temp./repmat(sum(temp, 2), 1, size(p_w, 2));
Your first solution with the loops is wrong, as I have explained in my first answer. You can check this using
sum(p_w, 2)
0 件のコメント
その他の回答 (3 件)
Thorsten
2014 年 11 月 24 日
編集済み: Thorsten
2014 年 11 月 24 日
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.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!