Value of column, based on function over another column in Matlab table -
i'm interested in value of result in same row min value of each column (and have many columns, loop on them, or rowfun not know how 'result' then).
table 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 | +----+------+------+----+------+------+--------+
solution
8 10 12 10 12 8
i know can apply rowfun, don't know how result. , then, can this, cannot loop on columns:
a(cell2mat(a.x1) == min(cell2mat(a.x1)), 7)
and have tried several ways of making variable can't make work, that:
a(cell2mat(variable) == min(cell2mat(variable)), 7)
thank you!
assuming data homogeneous can use table2array
, second output of min
index results:
% set 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 = table2array(t); % when passed matrix, min finds minimum of each column default % exclude results column, assumed last [~, minrow] = min(a(:, 1:end-1)); solution = t.result(minrow)'
which returns:
solution = 8 10 12 10 12 8
from documentation min
:
m = min(a)
returns smallest elements ofa
.
<snip>
- if
a
matrix,min(a)
row vector containing minimum value of each column.
Comments
Post a Comment