Effacer les filtres
Effacer les filtres

Delete columns in a structure array

5 vues (au cours des 30 derniers jours)
SS
SS le 28 Juin 2021
Modifié(e) : SS le 29 Juin 2021
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')

Réponse acceptée

Stephen23
Stephen23 le 28 Juin 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 commentaires
Stephen23
Stephen23 le 29 Juin 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);
SS
SS le 29 Juin 2021
Modifié(e) : SS le 29 Juin 2021
Thanks a lot.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by