Max-min of a matrix column over rows where other columns have given values
조회 수: 3 (최근 30일)
이전 댓글 표시
Problem A. A is a matrix. I want to pick the matrix rows on which columns 2:9 have certain values and compute the max-min of column 10 over those rows.
[Even an efficient solution to just problem A would be very nice. Even better, if somebody solves the whole problem (problem B) that I describe below.]
Problem B1. I go through all possible value combinations in columns 2:9, and pick a the max-min for each such combination as in "problem A" above. Then I take the max over all those "max-min" values.
[I call this "the maximal effect of parameter 1 (column 1) to column 10 (output 10)". Indeed, columns 1:9 are considered parameters (or "inputs") and columns 10:20 are outputs or "test results".]
I also want to get the effects on columns 11:20, and also the effects of columns 2:9 (in place of column 1).
Problem B2. [This is the same as problem B1, but I describe it using different words.]
A is a matrix. Columns 1:9 are parameters and columns 10:20 are outputs.
For any o in [10,20] and any parameter values p=(p1,p2,...,p9), let O(p;o) be the set of values A(k,o), where the row number k is such that A(k,j)=pj for all j. That is: the value of output o on the lines that have parameters p.
For each parameter pk, I want to know its maximal effect on the outputs. That is:
effect(1,o) = max_{p2,p3,...,p9} (max(P(:,p2,p3,...,p9;o) - min(A(:,p2,p3,...,p9;o))
So for each fixed values of the other parameters p2, p3, ..., p9, we check how big and small o-value we get by changing just p1. Their difference is the max-min (the effect of p1 with those fixed p2,p3,...,p9).
Then effect(1,o) is the maximum of these effects (over all possible fixed fixed p2,p3,...,p9).
Analogously for the other parameters 2,...,9.
Solution B. This code solves problem B1 = problem B2 (hence also A). However, I believe that this code is much slower than a medium-level Matlab worker could produce; I just don't know that much of Matlab. (My true A has many lines and more columns, and the same might apply to many readers.)
(This code works even if there are many rows with the same parameter combination (so the outputs need not be uniquely determined by the parameters but may differ even with same parameters).)
Nrows = size(A,1); %A is a Nrows*20 matrix.
Npars = 9; %We consider columns 1:9 parameters
Nouts = 11;
effect = zeros(Npars, Nouts); %effects of 9 pars on 11 outputs
for n = 1:9
otherpars = [1:n-1 n+1:Npars]; %all pars but the n'th
B = unique(A(:,otherpars),'rows'); %Kx(Npar-1) matrix of all par columns but the n'th (all unique value combinations)
K = size(B,1); %number of different combinations of otherpars
doutsK = zeros(K, Nouts); %doutsK(k,nout) = to be max(nout)-min(nout) of for the k'th otherpars combination
for k=1:K % otherpar combinations
ind = ismember(A(:, otherpars), B(k,:), 'rows'); %Nrows*1 vector. "1" on rows having the k'th combination of otherpars, "0" on others.
doutsK(k, :) = max(A(ind, Npars+1:end), [], 1) - min(A(ind, Npars+1:end), [], 1); %max-min for that k'th combination (row vector)
end
effect(n, :) = max(doutsK, [], 1); % max_otherparcombinations (max-min spread) (1xNouts row vector)
end
댓글 수: 3
채택된 답변
Jan
2022년 10월 25일
편집: Jan
2022년 10월 25일
In addition to my comment: max(x)-min(x) expressed as range(x):
Nrows = size(A, 1);
Npars = 9;
Nouts = 11;
effect = zeros(Npars, Nouts);
other = true(1, Npars);
AEnd = A(:, Npars+1:end);
for n = 1:9
other(n) = false; % Logical indexing
AA = A(:, other); % Create the submatrix once only
other(n) = true; % restore logical index vector
B = unique(AA, 'rows');
K = size(B, 1);
doutsK = zeros(1, Nouts); % Do not collect a matrix just to obtain the maximum
for k = 1:K
ind = all(AA == B(k, :), 2);
Aind = AEnd(ind, :);
doutsK = max(doutsK, range(Aind, 1));
end
effect(n, :) = doutsK;
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!