Delete columns in a structure array
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello.
I have a structure array Data (1 x 500,000) with 10 fields f1, f2,.......f10. I want to delete all the columns i.e., Data(i) whose f10 > 100.
For example, if the below is the input:
Data(1).f1=[10,70,30 40,50,60], Data(1).f2=[100,20,50,60,70,140],......Data(1).f10=[-10,20,-50,42,-70,140] ;
Data(2).f1=[16,98,74,47,99], Data(2).f2=[101,54,69,20,11],.......Data(2).f10=[17,-54,69,-20,37];
Data(3).f1=...... , Data(3).f2=.....,........ Data(3).f10=...........;
Data(4).f1=.... , Data(4).f2=....., ....... Data(4).f10=............;
.
.
Data(i).f1=...., Data(i).f2=.... and Data(i).f10=............;
In Data(1).f10 there is an entry 140 which is greater than 100 so, I want to delete the whole column Data(1).
I have tried below option but, it does not help.
for i=1:length(Data)
i
if (Data(i).f10 > 100)
Data(i)=[]
end
end
save('Data.mat')
0 Kommentare
Akzeptierte Antwort
Stephen23
am 28 Jun. 2021
Assuming that the data in f10 is scalar numeric (you did not tell us this important information):
idx = [Data.f10]>10;
Data = Data(~idx)
save('Data.mat','Data')
3 Kommentare
Stephen23
am 29 Jun. 2021
"This is a bit confusing."
It is not very confusing: in your original question you omitted to give very important information, such as the sizes of the structure fields. Now that you have edited your question, we can make some progress:
fun = @(s) any(s.f10(:)>10);
idx = arrayfun(fun,Data);
Data = Data(~idx);
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!