-2

I have a table with 5 columns.

Value, Quantity, colors, location, tolerance

I want to delete a complete row, when one quantity is bigger then the other while searching with same Value. Like

If I have Value= 1000, Quantity = 500, Color = white, Location = desk, tolerance = 0.05

And I try to Add another row, with Value 1000, and other quantity, I need to sum then and delete the past row.

example

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Marco
  • 339
  • 2
  • 11
  • 20

1 Answers1

0

Ok I think I got what you are after, although to be honest your question is quite vague. However, It's not the very efficient to create data structures like your example. But the following code will allow you to do it

1 . generate some fake data (next time you ask please embed your code)

Value = [1000;500;45;60;75];
Tol = [.05;.05;.05;.05;.05];
foo = {'blabla';'blablabla';'blabla';'bla';'bla'};
foo2 = [176;163;131;133;119];
loc = {'blabla';'blablabla';'blabla';'bla';'bla'};

T = table(Value,Tol,foo,foo2,loc)

% T = 
% 
% Value    Tol         foo        foo2        loc    
% _____    ____    ___________    ____    ___________
% 
% 1000     0.05    'blabla'       176     'blabla'   
%  500     0.05    'blablabla'    163     'blablabla'
%   45     0.05    'blabla'       131     'blabla'   
%   60     0.05    'bla'          133     'bla'      
%   75     0.05    'bla'          119     'bla'     
newentrys  = table([1000;510],[.01;0.7],{'blabla';'bloblo'},[190;340],{'blabla';'bloblo'})

% newentrys = 
% Var1    Var2      Var3      Var4      Var5  
% ____    ____    ________    ____    ________
% 
% 1000    0.01    'blabla'    190     'blabla'
% 510     0.7    'bloblo'    340     'bloblo'

for ii=1:height(newentrys)
    tmp = newentrys(ii,:);
    if sum(tmp.Var1==T.Value)>0
        ix = find(tmp.Var1==T.Value);
        T(ix,:) = tmp;% if you found duplicate values replace this with the new value (if you want to do something else this is the place)
    else
        T(end+1,:) = tmp; % just add it to the table 
    end
end

% T = 
% 
% Value    Tol         foo        foo2        loc    
% _____    ____    ___________    ____    ___________
% 
% 1000     0.01    'blabla'       190     'blabla'   
%  500     0.05    'blablabla'    163     'blablabla'
%   45     0.05    'blabla'       131     'blabla'   
%   60     0.05    'bla'          133     'bla'      
%   75     0.05    'bla'          119     'bla'      
%  510      0.7    'bloblo'       340     'bloblo'  
eyalsoreq
  • 358
  • 1
  • 9