存储最小值的矩阵索引
我有一个二维矩阵Ac(yr,j)
。
我希望它将它的每个值与另一个一维数组进行比较,并存储即将到来的绝对最小值的数组值。
for yr=1:32,
for j=1:12,
for in=1.5:1:32.5,
actin=Ac(yr,j);
kar(in-0.5)=abs(in-actin);
dab(yr,j)=min(kar(kar>=0));
end
end
end
我能够找到最小的正值,但找不到它即将到来的 in
的值。
I have a 2d matrix Ac(yr,j)
.
I want it to compare each value of it with another 1D array and store the value of array for which absolute minimum is coming.
for yr=1:32,
for j=1:12,
for in=1.5:1:32.5,
actin=Ac(yr,j);
kar(in-0.5)=abs(in-actin);
dab(yr,j)=min(kar(kar>=0));
end
end
end
I'm able to find the least positive value but not the value of in
for which it is coming.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要如下所示调用 max 才能获取 索引而不是值。
为了摆脱嵌套循环,您可以尝试 arrayfun。定义要对每个数组元素执行的操作。
执行定义的操作。
You need to call max as shown below in order to get the index instead of the value.
In order to get rid of the nested loops you could try arrayfun. Define the operation to be performed on every array element.
Execute the defined operations.
您的代码需要工作,我猜测
in
是您要比较的数组。首先,您可以摆脱第三个 for 循环,然后执行以下操作:
最后一个表达式让我困惑:
kar 总是 >=0 (来自 abs 函数),所以你不需要检查它......更糟糕的是,它总是返回 1!所以你总是会得到 kar 的第一个索引。你尝试过吗:
?
your code needs work, and I'm guessing
in
is the array you want to compare.to start off, you can get rid of the third for loop and just do:
the last expression puzzles me:
kar is always >=0 (from the abs function) so you don't need to check for it... worse, it will always return 1! so yo u will always get the first index of kar. Have you tried:
?