How to conditionally merge multiple variables in a table

조회 수: 1 (최근 30일)
Blue
Blue 2021년 7월 21일
답변: Star Strider 2021년 7월 21일
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)

답변 (1개)

Star Strider
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 = 10×3 table
var_a var_b Var3 _____ _____ _________________ 1 a 1 1 NaN 2 b 2 NaN 2 3 c 3 NaN NaN 4 d 4 4 4 5 e NaN NaN NaN 6 f NaN NaN NaN 7 g 7 7 NaN 8 h 80 8 NaN 9 i 9 9 9 10 k 10 90 10
t.Properties.VariableNames{3} = 'new_var'
t = 10×3 table
var_a var_b new_var _____ _____ _________________ 1 a 1 1 NaN 2 b 2 NaN 2 3 c 3 NaN NaN 4 d 4 4 4 5 e NaN NaN NaN 6 f NaN NaN NaN 7 g 7 7 NaN 8 h 80 8 NaN 9 i 9 9 9 10 k 10 90 10
.

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by