[英]Value of column, based on function over another column in Matlab table
我对与每一列的最小值在同一行中的结果的值感兴趣(而且我有很多列,所以我想遍历它们,或者进行rowfun,但我不知道如何获得'结果”)。
表A
+----+------+------+----+------+------+--------+ | x1 | x2 | x3 | x4 | x5 | x6 | result | +----+------+------+----+------+------+--------+ | 1 | 4 | 10 | 3 | 12 | 2 | 8 | | 10 | 2 | 8 | 1 | 12 | 3 | 10 | | 5 | 10 | 5 | 4 | 2 | 10 | 12 | +----+------+------+----+------+------+--------+
解
8 10 12 10 12 8
我知道我可以申请rowfun,但是后来我不知道如何获得结果。 然后,我可以执行此操作,但是不能遍历所有列:
A(cell2mat(A.x1) == min(cell2mat(A.x1)), 7)
并且我尝试了几种方法来使它成为变量,但是我无法使其起作用,因此:
A(cell2mat(variable) == min(cell2mat(variable)), 7)
谢谢!
假设您的数据是同质的,则可以使用table2array
和min
的第二个输出来索引结果:
% Set up table
x1 = [1 10 5];
x2 = [4 2 10];
x3 = [10 8 5];
x4 = [3 1 4];
x5 = [12 12 2];
x6 = [2 3 10];
result = [8 10 12];
t = table(x1.', x2.', x3.', x4.', x5.', x6.', result.', ...
'VariableNames', {'x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'result'});
% Convert
A = table2array(t);
% When passed a matrix, min finds minimum of each column by default
% Exclude the results column, assumed to be the last
[~, minrow] = min(A(:, 1:end-1));
solution = t.result(minrow)'
哪个返回:
solution =
8 10 12 10 12 8
从min
的文档中:
M = min(A)
返回的最小元素A
。
<snip>
- 如果
A
是矩阵,则min(A)
是包含每一列最小值的行向量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.