回 帖 发 新 帖 刷新版面

主题:请问怎样用一个数组做子函数参数

如题。

思路:

一个数组有24个元素,想通过一个排序函数(子函数)最终得到这个数组的最大值。

非常谢谢!

回复列表 (共3个回复)

沙发

请用maxval,谢谢

板凳


maxval???

请详解!!!

3 楼

[quote]
maxval???

请详解!!![/quote]
MAXVAL

Transformational Intrinsic Function (Generic): Returns the maximum value of all elements in an array, a set of elements in an array, or elements in a specified dimension of an array.

Syntax
result = MAXVAL (array[,dim] [,mask])

array
 (Input) Must be an array of type integer or real. 
 
dim
 (Input; optional) Must be a scalar integer expression with a value in the range 1 to n, where n is the rank of array. 
 
mask
 (Input; optional) Must be a logical array that is conformable with array.
 

Results
The result is an array or a scalar of the same data type as array.

The result is a scalar if dim is omitted or array has rank one.

The following rules apply if dim is omitted: 

If MAXVAL( array) is specified, the result has a value equal to the maximum value of all the elements in array. 

If MAXVAL( array, MASK= mask) is specified, the result has a value equal to the maximum value of the elements in array corresponding to the condition specified by mask.


The following rules apply if dim is specified: 

An array result has a rank that is one less than array, and shape (d1, d2,...,ddim-1, ddim+1, ..., dn), where (d1, d2, ..., dn) is the shape of array. 

If array has rank one, MAXVAL( array, dim[, mask]) has a value equal to that of MAXVAL( array[,MASK = mask]). Otherwise, the value of element (s1, s2, ..., sdim-1, sdim+1, ..., sn) of MAXVAL( array, dim, [, mask]) is equal to MAXVAL( array(s1, s2, ..., sdim-1, :, sdim+1, ..., sn) [,MASK = mask(s1, s2, ..., sdim-1, :, sdim+1, ..., sn)]).


If array has size zero or if there are no true elements in mask, the result (if dim is omitted), or each element in the result array (if dim is specified), has the value of the negative number of the largest magnitude supported by the processor for numbers of the type and kind parameters of array.

Example
The value of MAXVAL ((/2, 3, 4/)) is 4 because that is the maximum value in the rank-one array.

MAXVAL (B, MASK=B .LT. 0.0) finds the maximum value of the negative elements of B.

C is the array

  [ 2  3  4 ]  [ 5  6  7 ].MAXVAL (C, DIM=1) has the value (5, 6, 7). 5 is the maximum value in column 1; 6 is the maximum value in column 2; and so forth.

MAXVAL (C, DIM=2) has the value (4, 7). 4 is the maximum value in row 1 and 7 is the maximum value in row 2.

The following shows another example:

 INTEGER array(2,3), i(2), max INTEGER, ALLOCATABLE :: AR1(:), AR2(:) array = RESHAPE((/1, 4, 5, 2, 3, 6/),(/2, 3/)) ! array is   1 5 3 !            4 2 6 i = SHAPE(array)      ! i = [2 3] ALLOCATE (AR1(i(2)))  ! dimension AR1 to the number of                       ! elements in dimension 2                       ! (a column) of array ALLOCATE (AR2(i(1)))  ! dimension AR2 to the number of                       ! elements in dimension 1                       ! (a row) of array max = MAXVAL(array, MASK = array .LT. 4) ! returns 3 AR1 = MAXVAL(array, DIM = 1)  ! returns [ 4 5 6 ] AR2 = MAXVAL(array, DIM = 2)  ! returns [ 5 6 ] END

我来回复

您尚未登录,请登录后再回复。点此登录或注册