How to merge two columns of a table into a single column
1 view (last 30 days)
Show older comments
Cheerful
on 12 Oct 2016
Answered: Walter Roberson
on 12 Oct 2016
Dear Experts,
I have a table, tableA of 3 columns. There are empty values in the first 2 columns and I want to merge the first 2 columns into a new columns such that there are no empty values.The table is shown below
Asset1 Asset2 Score
Appl Appl 10
IBM " 2
" Google 5
Dell Dell 4
Desired output
Asset Score
Appl 10
IBM 2
Google 5
Dell 4
I think "join" or "outerjoin" wont be able to do it.
Thanks
0 Comments
Accepted Answer
Walter Roberson
on 12 Oct 2016
Letting T be the table:
v = cellfun(@isempty, T.Asset1)+1;
r = [T.Asset1, T.Asset2];
good_asset = r(sub2ind(size(r), (1:size(r,1)).', v));
result = table(good_asset, T.Score, 'VariableNames', {'Asset', 'Score'});
0 Comments
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!