我在 MATLAB 中有三列数据...
A = [ 10.0000 10.0000 22.9401;
9.0000 11.0000 302.5402;
9.0000 11.0000 285.7156;
9.0000 11.0000 294.7599;
9.0000 11.0000 301.2963;
9.0000 11.0000 288.3905;
9.0000 11.0000 301.0232;
9.0000 11.0000 300.4630;
9.0000 11.0000 287.3316;
9.0000 11.0000 265.4248;
9.0000 11.0000 297.4152;
11.0000 11.0000 32.7959;
11.0000 11.0000 32.2272;
10.0000 12.0000 304.5999;]
我需要返回满足许多条件的行。
对于要包含的任何行,第一列和第二列必须相等。
然后我想要第 2 列中的最大值。
如果有两个候选人满足条件1和2,我想选择第3栏中数字最小的一个。
所以在上面的例子中,我想输入数据矩阵,它返回 13。
您可以主要使用逻辑索引来做到这一点
% Add a row index as a 4th column to A.
% This is so we can track the original row number as we filter A down.
A(:,4) = (1:size(A,1)).';
% Filter A to satisfy condition (1); column 1 value == column 2 value
Afilt = A( A(:,1) == A(:,2), : );
% Filter again to leave only the rows which satisfy condition (2)
% Also sort on column 3 so that row 1 has the lowest value in column 3
Afilt = sortrows( Afilt( Afilt(:,1) == max(Afilt(:,1)), : ), 3, 'ascend' );
% Get the first value in column 4, which corresponds to the lowest value in col 3
idx = Afilt(1,4);
你可以在一行中完成所有这些操作,但它并不漂亮,并且您正在计算条件 1 两次:
[~,idx] = min( A(:,3) .* 1./(A(:,1)==A(:,2) & A(:,1)==max(A(A(:,1)==A(:,2),1))) );
您可以连续测试所有条件:
% First column = Second column
ind1 = find(A(:,1) == A(:,2))
% Find the maximum value from the second column that also meet the first condition
ind2 = find(A(ind1,2) == max(A(ind1,2)))
% Get the minimal value on the last column that also meet the first and second condition
[~,ind3] = min(A(ind1(ind2),3))
% Get the result
row = ind1(ind2(ind3))