2014-03-05 100 views
0

我具有這樣的結構:從結構刪除元素

p=struct('exponent',{1,2,2,4},'coeff',{2,5,6,7}) 

我試圖以除去指數字段(2)的所述第三元件與所述coeff場(6),以使它們AREN 「T的空的空間是這樣的:

p=struct('exponent',{1,2,[],4},'coeff',{2,5,[],7}) 

但變成長度爲3的這樣

p=struct('exponent',{1,2,4},'coeff',{2,5,7}) 
012的結構

我不確定如何做到這一點。

+0

你是怎麼做到的? – herohuyongtao

回答

0
  1. 簡單的方法:

    p(3) = []; 
    
  2. 另一種方法,它允許更多的靈活性(例如,你可以刪除的第三指數和第二係數,如果這是有道理的):

    e = [p.exponent]; %// convert to vector 
    e = num2cell(e([1:2 4:end])); %// remove third and convert to cell array 
    c = [p.coeff]; 
    c = num2cell(c([1:2 4:end])); %// or remove any other than the third here 
    p = struct('exponent',e,'coeff',c) %// recreate p 
    
+0

感謝您的快速回復和非常好的解釋 – user3356728