我有一个矩阵,可以这样说
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如何获取最小值和矩阵中的索引
发布评论