How to conditionally merge multiple variables in a table
2 次查看(过去 30 天)
显示 更早的评论
Hi,
In the table t below I would like to merge the variables var_c, var_d and var_e into a single new variable called new_var. For a given row, the highest value of the 3 column should be kept. If 2 or 3 values are equal for a given row then it doesnt matter which one is kept. The desired output is desired_output. How would I do that ?
Thank you,
var_a = [1:10]';
var_b = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k']';
var_c = [1, 2, 3, 4, NaN, NaN, 7, 80, 9, 10]';
var_d = [1, NaN, NaN, 4, NaN, NaN, 7, 8, 9, 90]';
var_e = [NaN, 2, NaN, 4, NaN, NaN, NaN, NaN, 9, 10]';
t = table(var_a, var_b, var_c, var_d, var_e)
new_var = [1, 2, 3, 4, NaN, NaN, 7, 80, 9, 90]';
desired_output = table(var_a, var_b, new_var)
0 个评论
回答(1 个)
Star Strider
2021-7-21
Concatenate them horizontally uising square brackets [], then assign the name to the new variable —
var_a = [1:10]';
var_b = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k']';
var_c = [1, 2, 3, 4, NaN, NaN, 7, 80, 9, 10]';
var_d = [1, NaN, NaN, 4, NaN, NaN, 7, 8, 9, 90]';
var_e = [NaN, 2, NaN, 4, NaN, NaN, NaN, NaN, 9, 10]';
t = table(var_a, var_b, [var_c, var_d, var_e])
t.Properties.VariableNames{3} = 'new_var'
.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multirate Signal Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!