How to find the maximum value of the first column in multiple matrices excluding the row we selected?

6 ビュー (過去 30 日間)
chan
chan 2021 年 7 月 29 日
回答済み: Walter Roberson 2024 年 11 月 13 日 3:50
Suppose i have created a matrix say X=rand([6,3,2]);
How to find the maximum element in the first column of both the matrices excluding the row we selected ???
e.g: Here i am considering 1:6 (row) as nodes.Suppose i have selected node 2, then we ve to find max element from node 1,3,4,5,6 in the first column. Similarly, from the second matrix also we need to find the maximum element from the first column excluding the row we selected.

回答 (2 件)

Hari
Hari 2024 年 11 月 13 日 3:07
Hi chan,
I understand that you have a 3D matrix X with dimensions [6, 3, 2], and you want to find the maximum value in the first column of each 2D matrix while excluding a specified row.
I assume you want to perform this operation for both matrices along the third dimension of X and that the selected row is consistent across both matrices.
  • Select the Row to Exclude: Determine the index of the row you want to exclude. For example, if you want to exclude the second row, set "selectedRow = 2"
  • Extract the First Column: For each matrix in the third dimension, extract the first column and exclude the selected row.
selectedRow = 2; % Example: exclude the second row
matrix1 = X(:,:,1);
matrix2 = X(:,:,2);
  • Remove the Selected Row: Remove the specified row from the first column of each matrix.
firstColMatrix1 = matrix1(:,1);
firstColMatrix1(selectedRow) = []; % Exclude the selected row
firstColMatrix2 = matrix2(:,1);
firstColMatrix2(selectedRow) = []; % Exclude the selected row
  • Find the Maximum Value: Compute the maximum value of the remaining elements in the first column for each matrix.
maxValMatrix1 = max(firstColMatrix1);
maxValMatrix2 = max(firstColMatrix2);
  • Display the Results: Output the maximum values for each matrix.
disp(['Maximum in first column of matrix 1 (excluding row ', num2str(selectedRow), '): ', num2str(maxValMatrix1)]);
disp(['Maximum in first column of matrix 2 (excluding row ', num2str(selectedRow), '): ', num2str(maxValMatrix2)]);
Output for a sample matrix generated (using "random" function):
X(:,:,1) =
0.9791 0.4139 0.7391
0.5493 0.4923 0.9542
0.3304 0.6947 0.0319
0.6195 0.9727 0.3569
0.3606 0.3278 0.6627
0.7565 0.8378 0.2815
X(:,:,2) =
0.2304 0.3488 0.7311
0.7111 0.4513 0.1378
0.6246 0.2409 0.8367
0.5906 0.7150 0.1386
0.6604 0.8562 0.5882
0.0476 0.2815 0.3662
Maximum in first column of matrix 1 (excluding row 2): 0.97913
Maximum in first column of matrix 2 (excluding row 2): 0.66044
References:
Hope this helps!

Walter Roberson
Walter Roberson 2024 年 11 月 13 日 3:50
X = rand([6,3,2]);
Selected_Row = 2;
max_by_column = max(X(setdiff(1:end, Selected_Row), :, :), [], 1)
max_by_column =
max_by_column(:,:,1) = 0.9760 0.8972 0.9155 max_by_column(:,:,2) = 0.7232 0.9829 0.8969

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by