我有两个向量,我想找到向量b的哪个值最小化a的每个元素的和(a-b)。我想要的输出是一个长度为a的向量,包含向量b的值。
@using (Html.BeginForm())
{
@Html.TextBoxFor(a=>a.Email)
<input type="submit"/>
}
<!-- your existing code for the modal dialog markup goes here-->
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-body">
<h2><p style="color: red;">@TempData["message"]</p> </h2>
</div>
</div>
<!-- Add the markup needed to render the modal dialog ends here-->
@section Scripts
{
<script>
$(function() {
var r = '@TempData["message"]';
if (r.length>0)
{
$('#myModal').modal('show');
}
});
</script>
}
我试过了 -
static int count1,count2;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TextBox1.Text = minima;
Label1.Text = "Αυτή την στιγμή χρησιμοποιούν τον ιστότοπο " + Convert.ToString(Application["UserCount"]) + " χρήστες";
count1 = 0;
count2 = 0;
}
}
但结果是gobbledygook。
所有帮助非常感谢。
答案 0 :(得分:1)
我们可能需要考虑函数abs
closest_longitude
closest_longitude <- function (x, y) {
which.min(abs(x - y))
}
sapply(a, closest_longitude, b)
#[1] 1 1 8 4 2 2 7 7 5
如果我们不使用abs
,则始终为5
sapply(a, closest_longitude, b)
#[1] 5 5 5 5 5 5 5 5 5
因为&#39; b&#39;是66,它将给出最低的负值,同时减去每个元素的&#39; a&#39;并选择了那个。
答案 1 :(得分:0)
这是&#34;蛮力&#34;算法
# 1. Vectors
a <- c(1, 3, 5, 7, 9, 12, 19, 25, 80)
b <- c(2, 9, 8.4, 7, 66, 32, 19, 4)
# 2. Init settings
sum_a_i_b_i_min <- Inf
a_i_min <- 0
b_i_min <- 0
# 3. Calculations
for(a_i in a){
for(b_i in b){
if(sum(a_i - b_i) < sum_a_i_b_i_min){
sum_a_i_b_i_min <- sum(a_i - b_i)
a_i_min <- a_i
b_i_min <- b_i
}
print(paste0(a_i, " : ", b_i, " : ", sum(a_i - b_i)))
}
}
# 4. Result
print(paste0("a_i_min = ", a_i_min, " / b_i_min = ", b_i_min,
" / sum(a_i_min - b_i_min) = ", sum_a_i_b_i_min))