Modifying a structure array
조회 수: 45 (최근 30일)
이전 댓글 표시
Hi. I am working with a structure array, and I want to modify the structure based on a condition on the field entries.
For example,
Here is the input,
S(1).f1=[1:100] and S(2).f2=[2:101];
S(2).f1=[40:120] and S(2).f2=[60:140]
S(3).f1=...... and S(3).f2=.....
S(4).f1=.... and S(4).f2=.....
.
.
S(i).f1=.... and S(i).f2=....
Can anyone suggest me some smart way to do this.
Conditions on input Output
S(1).f1>=0 and S(1).f1<=50 then ------> S(1).f1=[1:50] and S(1).f2=[2:51]
S(1).f1>50 and S(1).f1<=100 then ------>S(2).f1=[51:100] and S(2).f2=[52:101]
.... so on
Now, the same process for original/input S(2).f1, S(3).f1 .... S(i).f1 to re-form the structure.
After, modification the final step is to delete S(i) if the size of S(i).f1 is < 5.
댓글 수: 3
채택된 답변
Bruno Luong
2019년 8월 12일
편집: Bruno Luong
2019년 8월 12일
S=struct;
S(1).f1=5*[2,3,4,5,6,7,8,10,14,16,18,20];
S(1).f2=5*[3,6,9,12,15,18,21,24,27,30,33,36];
S(2).f1=40:120;S(2).f2=60:140;
S(3).f1=20:100;S(3).f2=40:120;
filtfun = @(s) s.f1>=0 & s.f1<=50; % adapt to your need
b = arrayfun(filtfun, S, 'unif', 0);
Sfilter = arrayfun(@(s,b) structfun(@(a) a(b{1}), s, 'unif', 0), S, b); % your reasult
I let you do the deletion step.
댓글 수: 12
Rik
2019년 8월 26일
Neither code section produces the error you indicate. You should try to find a data line that causes this error. That is one of the hallmarks of an MWE: the smallest section of data and code that produces the error.
Often when trying to create an MWE you already find the mistake on your own. I have sometimes started writing my own question, when I found the mistake when creating an MWE for my post.
Bruno Luong
2019년 8월 26일
편집: Bruno Luong
2019년 8월 26일
SS: "Hi again. I have tried to use the above code and unfortunately, it gives the follwoing error."
This error is expected to me because you wrote:
"For each S(i), the length of the field arrays is same throughout except 5 fields. The length of these 5 fields is 1 less than the other 25 field arrays. The last element in the these fields can be conisder as zero or empty, it doesn't matter."
Actually it does MATTER, contrary to what you wrote.
And I have warned you long ago.
"I let you deal with the exception of one less element on 5 fields (that's messy condition, and bad data structure design), you better fill them with trailing 0 or NaN, whatever suitable for you, to make all the fields have the same length."
추가 답변 (1개)
Rik
2019년 8월 11일
편집: Rik
2019년 8월 11일
Since you don't provide any indication of how you want this to work for struct array input, you'll have to modify this code yourself.
S=struct;
S(1).f1=[2,3,4,5,6,7,8,10,14,16,18,20];
S(1).f2=[3,6,9,12,15,18,21,24,27,30,33,36];
L=S(1).f1<=10;
if sum(L)>=5
S(2).f1=S(1).f1(L);
S(2).f2=S(1).f2(L);
end
S(1).f1(L)=[];S(1).f2(L)=[];
Edit:
%create input struct
S=struct;
S(1).f1=5*[2,3,4,5,6,7,8,10,14,16,18,20];
S(1).f2=5*[3,6,9,12,15,18,21,24,27,30,33,36];
S(2).f1=40:120;S(2).f2=60:140;
S(3).f1=20:100;S(3).f2=40:120;
%create the cell array that will hold the struct elements
c=cell(2,numel(S));
for n=1:numel(S)
c{1,n}=S(n);
L=S(n).f1<=50;
if sum(L)>=5
c{2,n}.f1=S(n).f1(L);
c{2,n}.f2=S(n).f2(L);
end
c{1,n}.f1(L)=[];c{1,n}.f2(L)=[];
end
%reshape back to a struct
S=[c{:}];
댓글 수: 4
참고 항목
카테고리
Help Center 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!