Apply Cellfun to only first column but keep second

Im starting with a two column cell array that Ive manipulated to clean up the data I have, pulled certain characters out of string text using regexp which created a 1 column array but I could concatenate its matching second column back. Now within this 2 column array the first column that I manipulated has some empty cells. I would liked to get rid of those empty cells in the first column and their correspnding row in the second column but keep both columns within an array.
Ex. I want to get ride of the first two rows since they are empty but keep the others along with their corresponding numbers in column 2.
[] 1
[] 1
'VWVPSSVMVV' 3
'RWAREVLQFA' 1
'ASLQQWVQVA' 4

 採用された回答

madhan ravi
madhan ravi 2019 年 1 月 11 日
編集済み: madhan ravi 2019 年 1 月 11 日

0 投票

Anytime :) , if you got the answer to your orignal question about removing empty row make sure to accept the answer.
Note: You can adapt the any two methods below according to your version.
Requires 2015b or later:
mycell={'VWVPSSVMVV' 3
'VWVPSSVMVV' 3
'RWAREVLQFA' 1
'ASLQQWVQVA' 4};
T=cell2table(mycell);
C=unique(T);
T.Properties.VariableNames={'Names','IDs'};
G=findgroups(T);
C.IDs=splitapply(@sum,T.IDs,G);
NewTable=C(:,[1 end]);
NewTable.Properties.VariableNames={'uniqueNames','sumIDs'}
Requires 2018a or later:
mycell={'VWVPSSVMVV' 3
'VWVPSSVMVV' 3
'RWAREVLQFA' 1
'ASLQQWVQVA' 4};
T=cell2table(mycell);
T.Properties.VariableNames={'Names','IDs'};
G = groupsummary(T,'Names','sum');
NewTable=G(:,[1 end])

2 件のコメント

Jacob Larson
Jacob Larson 2019 年 1 月 11 日
Thank you that is perfect! much more efficient than the loop I was trying
madhan ravi
madhan ravi 2019 年 1 月 11 日
Anytime :)

サインインしてコメントする。

その他の回答 (1 件)

madhan ravi
madhan ravi 2019 年 1 月 11 日

0 投票

mycell={[] 1
[] 1
'VWVPSSVMVV' 3
'RWAREVLQFA' 1
'ASLQQWVQVA' 4};
EXPECTED=mycell(all(~cellfun('isempty',mycell),2),:)
Gives:
EXPECTED =
3×2 cell array
{'VWVPSSVMVV'} {[3]}
{'RWAREVLQFA'} {[1]}
{'ASLQQWVQVA'} {[4]}

1 件のコメント

Jacob Larson
Jacob Larson 2019 年 1 月 11 日
Awesome, very helpful! Also is there a way to sum the second column values based on the IDs in the first and get rid of the repeated IDs?
Meaning for the examples below the ID 'VWVPSSVMVV' would sum to 6 in the second column and the ID would only appear one time.
{'VWVPSSVMVV'} {[3]}
{'RWAREVLQFA'} {[1]}
{'VWVPSSVMVV'} {[3]}
{'ASLQQWVQVA'} {[4]}

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by