Matlab如何获取最小值和矩阵中的索引 [英] matlab how to get min value and its index in a matrix

查看:580
本文介绍了Matlab如何获取最小值和矩阵中的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵,可以这样说

I have a matrix let's say like this

A=[1 3 6 2 0 4
   6 8 9 5 1 4
   7 2 7 8 9 2]

我想获得给定行(r)且列在间隔([c.. c+x])中的最小值.我也想要索引(它的列数). 我可以通过

I want to get the minimal value where the row is given (r) and the column is in an interval ([c.. c+x]). Also I want the index (number of column of it). I can get the min with

MinVal=min(A(r,c:c+x))

示例

MinVal=min(A(2,3:3+2))

会给我

 % MinVal= 1

此MinVal的索引为I= 5,因为它位于第5列(我已经知道该行了,不需要它).

The index of this MinVal is I= 5 since it is in the 5th column (I know already the row and don't need it).

但是如何获取该索引?

But how to get this index ?

如果我这样做,我就没有想要的东西

If I do like this, I don't get what I want

 [MinVal,I]=min(A(r,c:c+x))

推荐答案

它可能不是最短的代码,但是很容易理解:

It might not be the shortest code, but an easy to understand possibility:

创建一个掩码,指示您在子矩阵中使用哪些变量:

Create a mask indicating which variables you use in your submatrix:

 M=false(size(A));
 M(r,c:c+x)=true; %use the same indexing operation

转换为线性索引:

M=find(M);

并使用它将I转换为完整矩阵中的索引:

And use it to translate I to indices in the full matrix:

M(I)

这篇关于Matlab如何获取最小值和矩阵中的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆