Matlab如何获取最小值和矩阵中的索引

编程入门 行业动态 更新时间:2024-10-13 22:27:25
本文介绍了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如何获取最小值和矩阵中的索引

本文发布于:2023-11-07 10:41:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566233.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   索引   最小值   Matlab

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!