Delete some columns from structure array

1 次查看(过去 30 天)
Hi. I am working with a structure array S (1 X 50,000) with 10 fields in which some columns are multiuple entries ands sub-set of another column.
Here is the input,
S(1).f1=[10,20,30 40,50,60], S(1).f2=[100,20,50,60,70,140],.........S(1).f10=[11,22,33,44,55,66];
S(2).f1=[56,98,74,87,99] and S(2).f2=[101,54,69,20,11],....... S(2).f10=.........[54,55,65,74,90];
S(3).f1=...... and S(3).f2=.....
S(4).f1=.... and S(4).f2=.....
.
.
S(13).f1=[10,20,30 40,50,60,70,80,90,100], S(13).f2=[100,20,50,60,70,140,160,200,220,300],.........S(13).f10=[11,22,33,44,55,66,65,72,34,61];
.
.
.
As we can see S(1) is a sub-set of S(13). I want to write a loop to compare all the columns and retain only the ones with maximum length; meaning that to delete S(1) and retain S(13) in the above example.
Can someone please help me with this.

采纳的回答

Jan
Jan 2021-5-25
编辑:Jan 2021-5-25
toDel = false(1, numel(S));
for i1 = 1:numel(S)
data1 = S(i1).f1;
n1 = numel(data1);
for i2 = i1 + 1:numel(S)
if ~toDel(i2)
data2 = S(i2).f1;
n2 = numel(data2);
if n1 >= n2
if isequal(data1(1:n2), data2)
toDel(i2) = true;
end
elseif isequal(data1, data2(1:n1))
toDel(i1) = true;
end
end
end
end
S(toDel) = [];
Maybe "subset of" does not mean, that the first elements are equal. Then all(ismember()) might be, what you need instead of isequal.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by