如何在 MATLAB 中求多维矩阵的最大值或最小值?

发布于 2024-08-28 10:01:00 字数 123 浏览 1 评论 0原文

我在 MATLAB 中有一个 4D 测量值数组。每个维度代表不同的测量参数。我想找到最大值和最小值以及每个值的索引(即哪个参数)。

最好的方法是什么?我想我可以在每个维度上取最大值中的最大值,但这看起来像是一个拼凑。

I have a 4D array of measurements in MATLAB. Each dimension represents a different parameter for the measurement. I want to find the maximum and minimum value and the index (i.e. which parameter) of each.

What's the best way to do it? I figure I can take the max of the max of the max in each dimension, but that seems like a kludge.

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

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

发布评论

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

评论(2

陌伤浅笑 2024-09-04 10:01:00

简单的例子:

%# random 4 d array with different size in each dim
A = rand([3,3,3,5]);

%# finds the max of A and its position, when A is viewed as a 1D array
[max_val, position] = max(A(:)); 

%#transform the index in the 1D view to 4 indices, given the size of A
[i,j,k,l] = ind2sub(size(A),position);

找到最小值作为练习:)。

继一条评论后:
如果您不知道数组 A 的维数,因此无法编写“[i,j,k,l] =”部分,请使用以下技巧:

indices = cell(1,length(size(A)));

[indices{:}] = ind2sub(size(A),position);

Quick example:

%# random 4 d array with different size in each dim
A = rand([3,3,3,5]);

%# finds the max of A and its position, when A is viewed as a 1D array
[max_val, position] = max(A(:)); 

%#transform the index in the 1D view to 4 indices, given the size of A
[i,j,k,l] = ind2sub(size(A),position);

Finding the minimum is left as an exercise :).

Following a comment:
If you do not know the number of dimensions of your array A and cannot therefore write the "[i,j,k,l] =" part, use this trick:

indices = cell(1,length(size(A)));

[indices{:}] = ind2sub(size(A),position);
嘿嘿嘿 2024-09-04 10:01:00

对于二维数组,假设我
您只需使用 min /max 函数两次即可。
n 维数组的 n 次。
例如:a=[2 3 4; 5 6 7; -2 7 87; 911 7 34];

for minimum:  min(min(a,[],1))
             ->  the answer will be -2. 

您也可以将最小/最大尺寸参数设置为 2。因为这是调用该函数两次,第二次是在 u 选择的维度的最小/最大元素向量上。

同样,您可以执行 (max(max(a,[],1)) 来找出最大值。

for two dimensional array, say I
you can just use the min /max function twice.
n times for n dimensional array.
eg: a=[2 3 4; 5 6 7; -2 7 87; 911 7 34];

for minimum:  min(min(a,[],1))
             ->  the answer will be -2. 

you can put the dimension parameter in min/max to 2 as well. as this is calling the function twice, second time on the minimum/maximum element vector of the dimension u choose.

similarly, you can do (max(max(a,[],1)) to find out the maximum.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文