how to reduce the information contained in each field in a struct array?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
German Preciat Gonzalez
le 18 Jan 2017
Modifié(e) : Stephen23
le 19 Jan 2017
I have a very big struct array which can be summarized in something like this:
A = [0 0 0 0 0 0;
1 -1 0 0 0 0;
0 1 -1 1 0 0;
0 0 1 -1 0 -1;
0 0 1 0 -1 0];
structArray.A=A;
structArray.B={'B1[a]' 'B2[b]' 'B3[a]' 'B4[a]' 'B5[b]'}; % correspond to the rows
structArray.C={'C1' 'C2' 'C3' 'C4' 'C5' 'C6'}; % correspond to the columns
structArray.D={'D1' 'D2' 'D3' 'D4' 'D5'}; % correspond to the rows
structArray.E={'E1' 'E2' 'E3' 'E4' 'E5' 'E6'}; % correspond to the columns
If I use:
coolData=regexp(structArray.B,'(?<=\[).+?(?=\])','match')';
coolData=cellfun(@char, coolData);
coolerData=strmatch('a',coolData);
I will obtain all the rows relevant to me (all the rows with an "a" in the structArray.B), in this example:
coolerData =
1
3
4
I want to delete the all the columns from A and the corresponding data on structArray.C and structArray.E (which represent the columns) where the column and the rows obtained in "coolerData" are == to zero (in this example where all the elements in A([1 3 4],columns) are equal 0, therefore, columns 1 and 5) so I can obtain:
A = [ 0 0 0 0;
-1 0 0 0;
1 -1 1 0;
0 1 -1 -1;
0 1 0 0];
structArray.A=A;
structArray.B={'B1[a]' 'B2[b]' 'B3[a]' 'B4[a]' 'B5[b]'}; % correspond to the rows
structArray.C={'C2' 'C3' 'C4' 'C6'}; % correspond to the columns
structArray.D={'D1' 'D2' 'D3' 'D4' 'D5'}; % correspond to the rows
structArray.E={'E2' 'E3' 'E4' 'E6'}; % correspond to the columns
And then I want to delete all the row and the corresponding data on structArray.B and structArray.D (which represent the row) where all the row has only zeros (in this example row 1), so I can obtain:
A = [-1 0 0 0;
1 -1 1 0;
0 1 -1 -1;
0 1 0 0];
structArray.A=A;
structArray.B={'B2[b]' 'B3[a]' 'B4[a]' 'B5[b]'}; % correspond to the rows
structArray.C={'C2' 'C3' 'C4' 'C6'}; % correspond to the columns
structArray.D={'D2' 'D3' 'D4' 'D5'}; % correspond to the rows
structArray.E={'E2' 'E3' 'E4' 'E6'}; % correspond to the columns
I did that with a lot of FOR and IF but it looks ugly and take a lot of time (the size of the matrix I'm using it is 7867x12569) Do you have any suggestions, Thanks,
0 commentaires
Réponse acceptée
Guillaume
le 18 Jan 2017
Modifié(e) : Guillaume
le 19 Jan 2017
Function to filter a scalar structure:
function s = filterstruct(s)
rowstocheck = ~cellfun(@isempty, regexp(s.B, '\[a\]', 'once'));
colstodelete = all(s.A(rowstocheck, :) == 0);
s.A(:, colstodelete) = [];
s.C(colstodelete) = [];
s.E(colstodelete) = [];
rowstodelete = all(s.A == 0, 2);
s.A(rowstodelete, :) = [];
s.B(rowstodelete) = [];
s.D(rowstodelete) = [];
end
To then apply to a structure array:
filteredarray = arrayfun(@filterstruct, structArray);
That is assuming that you do have a structure array. Your example shows a scalar structure, not a structure array.
1 commentaire
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Structures 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!