MATLAB Answers

Replace empty element in table located in different rows

조회 수: 9(최근 30일)
Song Decn
Song Decn 26 Oct 2019
댓글: Song Decn 26 Oct 2019
ann = {'20191025';'20191025';'20190829'};
fann = {'20191025';'20191025';'20190829'};
eann = {'20190930';'20190930';'20190630'};
rec = {877134853.120000;877134853.120000;846524470.030000};
ass = {0;2692456847.78000;2712484638.45000};
cip = {0;88281897.8000000;63035038.2900000};
t = table(ann, fann, eann, rec, ass, cip);
Suppose I have a table like this
t =
3×6 table
ann fann eann rec ass cip
__________ __________ __________ ____________ ____________ ____________
'20191025' '20191025' '20190930' [8.7713e+08] [ 0] [ 0]
'20191025' '20191025' '20190930' [8.7713e+08] [2.6925e+09] [8.8282e+07]
'20190829' '20190829' '20190630' [8.4652e+08] [2.7125e+09] [6.3035e+07]
What I need to get is that if any value of "rec" "ass" "cip" is 0, then it should be replaced with non-zero element of another row with identical "ann" "fann" "eann" properties.
So the results for t would be
ann fann eann rec ass cip
__________ __________ __________ ____________ ____________ ____________
'20191025' '20191025' '20190930' [8.7713e+08] [2.6925e+09] [8.8282e+07]
'20190829' '20190829' '20190630' [8.4652e+08] [2.7125e+09] [6.3035e+07]
Is there a way to achieve this in a fast way?
Thank u very much

  댓글 수: 4

표시 이전 댓글 수: 1
Song Decn
Song Decn 26 Oct 2019
Thanks Guillaume, I think it's difficult to replace, after that using unique() can remove duplicated rows.
'rec' 'ass' 'cip' are strictly numeric arrays, like the sample code tells.
I think using serval for-loops can definitively solve the problem, but for-loops could be very slow in MATLAB. So maybe there is a better effecient way.
Thanks again.
Guillaume
Guillaume 26 Oct 2019
Loops won't be needed and replace or delete is probably just as easy.
Again, in your example, rec, ass, cip are cell arrays (where each cell contain a 1x1 double array. Compare the difference between
ann = {'20191025';'20191025';'20190829'};
fann = {'20191025';'20191025';'20190829'};
eann = {'20190930';'20190930';'20190630'};
rec = {877134853.120000;877134853.120000;846524470.030000};
ass = {0;2692456847.78000;2712484638.45000};
cip = {0;88281897.8000000;63035038.2900000};
t1 = table(ann, fann, eann, rec, ass, cip)
class(t1.rec)
rec = [877134853.120000;877134853.120000;846524470.030000];
ass = [0;2692456847.78000;2712484638.45000];
cip = [0;88281897.8000000;63035038.2900000];
t2 = table(ann, fann, eann, rec, ass, cip)
class(t2.rec)
The different storage makes a lot of difference. The latter one will make your life much easier. (And, since your first three columns appear to be dates, storing that as datetime would also make your life easier).
So, I ask again: are the numeric columns really stored as cell arrays (as you construct them in your example) or are they actually stored more sensibly as numeric vectors?
Song Decn
Song Decn 26 Oct 2019
t2 is how my data are stored, as numeric vectors. Ö

댓글을 달려면 로그인하십시오.

답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by