在 MATLAB 中查找矩阵子集的最大值,同时保留完整矩阵的索引

发布于 2024-10-14 13:30:51 字数 301 浏览 3 评论 0原文

目前,我可以使用以下代码找到矩阵 C 的最大值及其索引:

[max_C, imax] = max(C(:));
[ypeak, xpeak] = ind2sub(size(C),imax(1));

让我们调用矩阵 C_sub 的子集,

我想找到C_sub 的最大值,但我还想知道该最大值在 C 中的索引。

看起来这应该是一个简单的问题,但它却难倒了我。

感谢您的帮助!

Currently, I'm able to find the max value of a matrix C and its index with the following code:

[max_C, imax] = max(C(:));
[ypeak, xpeak] = ind2sub(size(C),imax(1));

Let's call a subset of the matrix C_sub

I want to find the max value of C_sub, but I also want to know the index of that max value in C.

Seems like it should be an easy problem, but it has me stumped.

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陌上芳菲 2024-10-21 13:30:51

创建

C_sub = C(rows,cols);

假设 C_sub 是由其中 rowscols 是索引向量 的。将这些 rowscols 向量保存在您可以重复使用的地方(如果您还没有这样做的话)。

[max_C_sub, ind_C_sub] = max(C_sub(:));
[ypeak_sub, xpeak_sub] = ind2sub(size(C_sub), ind_C_sub);
xpeak = cols(xpeak_sub);
ypeak = rows(ypeak_sub);

或者,如果 rows 和/或 cols 是逻辑向量而不是索引向量,则可以使用 find,然后按上述操作。

rows_ind = find(rows_logical);

Suppose that C_sub was created by

C_sub = C(rows,cols);

where rows and cols are vectors of indices. Save those rows and cols vectors somewhere you can reuse them, if you haven't already.

[max_C_sub, ind_C_sub] = max(C_sub(:));
[ypeak_sub, xpeak_sub] = ind2sub(size(C_sub), ind_C_sub);
xpeak = cols(xpeak_sub);
ypeak = rows(ypeak_sub);

Or if rows and/or cols was a vector of logicals instead of a vector of indices, you can convert them using find, and then proceed as above.

rows_ind = find(rows_logical);
神也荒唐 2024-10-21 13:30:51

如果您知道 C_sub 中最大值的索引,并且知道 C_subC 中的位置,您可以简单地将它们相加(并且对于 Matlab 索引减去 1)以获得相对于 C 的最大值的索引。

If you know the indices of the maximum in C_sub, and you know the position of C_sub within C, you can simply add them up (and subtract 1 for Matlab indexing) to get the indices of the maximum relative to C.

哭泣的笑容 2024-10-21 13:30:51

我曾经遇到过类似的问题,所以我编写了一个小实用程序来做到这一点。在文件交换中查找 Min2 和 Max2。这些工具允许您指定要搜索的给定矩阵的行和/或列的子集。

为自己做同样的事情。每当您需要 MATLAB 中的工具时,就编写它。不久之后,您就会建立一个适合您自己的特殊需求的漂亮工具箱。当然,首先查看文件交换,因为很可能您需要的内容已经写好并发布在那里。

I had a similar problem once, so I wrote a little utility to do this. Find Min2 and Max2 on the file exchange. These tools allow you to specify a subset of the rows and/or the columns of the given matrix to search over.

Do the same thing for yourself. Every time you need a tool in MATLAB, write it. Before long you will have built up a nice toolbox of tools tailored to your own special needs. Of course, look on the file exchange first, as there is a good chance that what you need has already been written and posted there.

深陷 2024-10-21 13:30:51

怎么样:

mask = nan(size(C));
mask(C_sub_indices) = 1;
[max_C, imax] = max(C .* mask);

在该代码中,C_sub_indices 是应用于生成 C_subC 的索引表达式。如果C_sub 不是C 的子矩阵(例如,如果它重新排列行或列),则此代码可能不起作用。

What about:

mask = nan(size(C));
mask(C_sub_indices) = 1;
[max_C, imax] = max(C .* mask);

In that code, C_sub_indices is the index expression applied to C that produced C_sub. This code may not work if C_sub is not a submatrix of C (e.g., if it rearranges the rows or columns).

欢你一世 2024-10-21 13:30:51

您还可以尝试这个脚本:

A=magic(5)
[x,y]=find(A==max(max(A))) %index maximum of the matrix A 
A_max=A(x,y)
[x1,y1]=find(A==min(max(A))) %index minimum of the matrix A 
A_min=A(x1,y1)

You can also try this script:

A=magic(5)
[x,y]=find(A==max(max(A))) %index maximum of the matrix A 
A_max=A(x,y)
[x1,y1]=find(A==min(max(A))) %index minimum of the matrix A 
A_min=A(x1,y1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文