-2

我有一个有 5 列的表。

价值、数量、颜色、位置、容差

我想删除一个完整的行,当一个数量大于另一个时,同时使用相同的值进行搜索。喜欢

如果我的值 = 1000,数量 = 500,颜色 = 白色,位置 = 桌面,容差 = 0.05

我尝试添加另一行,值为 1000 和其他数量,然后我需要求和并删除过去的行。

例子

4

1 回答 1

0

好吧,我想我得到了你所追求的,虽然老实说你的问题很模糊。但是,创建像您的示例这样的数据结构并不是很有效。但是下面的代码将允许你这样做

1. 生成一些假数据(下次你问请嵌入你的代码)

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'  
于 2015-10-25T09:03:41.620 回答