如何找到最大MATLAB中的多个阵列的? [英] How to find the maximum of multiple arrays in MATLAB?

查看:118
本文介绍了如何找到最大MATLAB中的多个阵列的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我们有一个数组 X 。我们可以发现这个数组的最大值如下:

Let's say we have an array x. We can find the maximum value of this array as follows:

maximum = max(x);

如果我有两个数组,我们说X和Y,我可以发现,通过使用命令包含最大值的阵列

If I have two arrays, let's say x and y, I can find the array that contains the maximum value by using the command

maximum_array = max(x, y);

让我们说这个数组为y。然后,我可以通过使用max命令与参数Y,和以前一样随x找到的最大值:

Let's say that this array is y. Then, I can find the maximum value by using the max command with argument y, as before with x:

maximum_value = max(y);

此两个步骤可以用下面的结构紧凑,单行命令执行:

This two-step procedure could be performed with the following compact, one-liner command:

maximum_value = max(max(x, y));

但是,当我们有2个以上的阵列会发生什么?据我所知,最大的功能不允许比较两个以上的阵列。因此,我必须用最大的双阵列,然后找到中间结果之间的最大(其中还涉及使用额外的变量)。当然,如果我有,比方说,50阵列,这将是 - 这真的是 - 一个tedius过程

But what happens when we have more than 2 arrays? As far as I know, the max function does not allow to compare more than two arrays. Therefore, I have to use max for pairs of arrays, and then find the max among the intermediate results (which involves also the use of additional variables). Of course, if I have, let's say, 50 arrays, this would be - and it really is - a tedius process.

有没有更有效的方法?

推荐答案

办法#1

并置沿其中矢量版本暗淡-2 ,然后使用maximium其值 最大 以及暗淡-2 来获得最大。

Concatenate column vector versions of them along dim-2 with cat and then use maximium values with max along dim-2 to get the max.

因此​​,假设 X 以Z 要输入数组,做这样的事情 -

Thus, assuming x, y and z to be the input arrays, do something like this -

%// Reshape all arrays to column vectors with (:) and then use cat
M = cat(2,x(:),y(:),z(:))

%// Use max along dim-2 with `max(..,[],2)` to get column vector 
%// version and then reshape back to the shape of input arrays
max_array = reshape(max(M,[],2),size(x))


方法2

您可以使用 为ndims 以寻找维数输入数组,然后沿着那就是尺寸加1 终于尺寸拼接找到最大沿着它以获得最大的价值数组。这将避免所有这些重塑来回,从而可以更有效和更紧凑的code,以及 -

You can use ndims to find the number of dimensions in the input arrays and then concatenate along the dimension that is plus 1 of that dimension and finally find max along it to get the maximum values array. This would avoid all of that reshaping back and forth and thus could be more efficient and a more compact code as well -

ndimsp1 = ndims(x)+1                         %// no. of dimensions plus 1
maxarr = max(cat(ndimsp1,x,y,z),[],ndimsp1)  %// concatenate and find max

这篇关于如何找到最大MATLAB中的多个阵列的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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