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(T(:)); %#transform the index in the 1D view to 4 indices, given the size of A [v,x,y,z] = ind2sub(size(T),position);

求三维矩阵 T 的最大值和最小值及其位置索引

T(:,:,1) = [3 3
            4 2];
T(:,:,2) = [6 8
            9 5];
T(:,:,3) = [16 18
            20 7];       
[max_val, position_max] = max(T(:)); 
[x,y,z] = ind2sub(size(T),position_max);
[min_val, position_min] = min(T(:)); 
[r,s,t] = ind2sub(size(T),position_min);
max_val = 20
x = 2
y = 1
z = 3
min_val = 2
r = 2
s = 2
t = 1
%# random 4D array with different size in each dimA = 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(T(:)); %#transform the index in the 1D view to 4 indices, given the size of A[v,x,. % 求矩阵中的最大值及其位置 [maxVal, maxIndex] = max(matrix(:)); [maxRow, maxCol] = ind2sub(size(matrix), maxIndex); % 求矩阵中的最小值及其位置 [minVal, minIndex] = min(matrix(:)); [minRow, minCol] = ind2sub(size(matrix), minIndex); % 打印最大值最小值及其位置 disp(['矩阵中的最大值为: ', num2str(maxVal)]); disp(['最大值位置为: (', num2str(maxRow), ', ', num2str(maxCol), ')']); disp(['矩阵中的最小值为: ', num2str(minVal)]); disp(['最小值位置为: (', num2str(minRow), ', ', num2str(minCol), ')']); 这段代码首先创建一个大小为5x5的随机矩阵,然后使用`max`函数和`min`函数分别求解矩阵中的最大值最小值。这两个函数返回的结果是向量,其中的元素表示最大值最小值矩阵中的位置。我们使用`ind2sub`函数将这个位置转换为行列坐标,并存储在`maxRow`、`maxCol`、`minRow`和`minCol`中。最后,使用`disp`函数打印最大值最小值及其位置