Replace empty element in table located in different rows

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 Commenti

It's most likely very easy to do. However, you say you want to replace but your desired output show the rows deleted. So which is it?
Also, 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. I.e. is the class of t.rec cell or double?
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.
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?
t2 is how my data are stored, as numeric vectors. Ö

Accedi per commentare.

Risposte (0)

Categorie

Richiesto:

il 26 Ott 2019

Commentato:

il 26 Ott 2019

Community Treasure Hunt

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

Start Hunting!

Translated by