Replace empty element in table located in different rows

1 view (last 30 days)
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 Comments
Guillaume
Guillaume on 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 on 26 Oct 2019
t2 is how my data are stored, as numeric vectors. Ö

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!