How to go from array to table for a variable that has to consider a different table configuration

1 visualizzazione (ultimi 30 giorni)
I am fairly new to MatLab and I need to convert the non index variables below to a table configuration rather than arrays in order to use the removeOutliersbyWords function but I am not sure how to do so. All the data for all variables is imported from a table that has Agemos, Sex, words, and 5 other variables that are represented by measuretoAnalyze = (i), i = 1:5
FemaleCases1 is what I tried next to what I am modifying it from, it has to consider the corresponding Index in order for the rest of the script to work.
femaleCasesIndex1 = Data(Data.Sex == 1,:);
femaleCases1 = Data(Data.Agemos(femaleCasesIndex1), Data.measureToAnalyze(femaleCasesIndex1),:);%.[AgeColumn(femaleCasesIndex1), DataColumn(femaleCasesIndex1)];
CarsCasesFemaleIndex1 = Data(Data.Sex == 1 & Data.Word == 1,:);
CarsCasesFemale1 = [AgeColumn(CarsCasesFemaleIndex1), DataColumn(CarsCasesFemaleIndex1)];
PopCasesFemaleIndex1 = Data(Data.Sex == 1 & Data.Word == 2,:);
PopCasesFemale1 = [AgeColumn(PopCasesFemaleIndex1), DataColumn(PopCasesFemaleIndex1)];
Duck1CasesFemaleIndex1 = Data(Data.Sex == 1 & Data.Word == 3,:);
Duck1CasesFemale1 = [AgeColumn(Duck1CasesFemaleIndex1), DataColumn(Duck1CasesFemaleIndex1)];
Duck2CasesFemaleIndex1 = Data(Data.Sex == 1 & Data.Word == 4,:);
Duck2CasesFemale1 = [AgeColumn(Duck2CasesFemaleIndex1), DataColumn(Duck2CasesFemaleIndex1)];
VowelCasesFemaleIndex1 = Data(Data.Sex == 1 & Data.Word == 5,:);
VowelCasesFemale1 = [AgeColumn(VowelCasesFemaleIndex1), DataColumn(VowelCasesFemaleIndex1)];
...
[CarsCases1, carscases1_outliersIndices] = removeOutliersbyWords( CarsCases1, measureToAnalyze, 'Agemos',2.576);
[PopCases1, popcases1_outliersIndices] = removeOutliersbyWords( PopCases1, measureToAnalyze, 'Agemos',2.576);
[Duck1Cases1, duck1cases1_outliersIndices] = removeOutliersbyWords( Duck1Cases1, measureToAnalyze, 'Agemos',2.576);
[Duck2Cases1, duck2cases1_outliersIndices] = removeOutliersbyWords( Duck2Cases1, measureToAnalyze, 'Agemos',2.576);
[VowelCases1, vowelcases1_outliersIndices] = removeOutliersbyWords( VowelCases1, measureToAnalyze, 'Agemos',2.576);

Risposte (1)

Avni Agrawal
Avni Agrawal il 14 Feb 2024
Hi Sophie,
I understand that you are trying to filter your data based on sex and word categories, and then convert these subsets into tables that can be used with a function `removeOutliersbyWords`. The code you have provided is trying to index into the `Data` table directly with logical conditions, which is correct for obtaining the indices, but the subsequent lines where you attempt to create new variables seem to be using a mix of indexing methods that are not appropriate for tables.
Here is how you can convert your filtered data into tables:
Firstly, ensure that `AgeColumn` and `DataColumn` are the names of the columns in your `Data` table that you want to include in your new tables. Replace them with the actual names of the columns if they are different. Then, for each subset of data, you can create a new table directly using the logical indices you have obtained. For example:
% Assuming 'Agemos' and 'measureToAnalyze' are the actual names of the columns in your Data table.
% Filter cases by sex
femaleCasesIndex1 = Data.Sex == 1;
femaleCases1 = Data(femaleCasesIndex1, {'Agemos', 'measureToAnalyze'}); % Replace 'measureToAnalyze' with actual column name
% Filter cases by sex and word category
CarsCasesFemaleIndex1 = Data.Sex == 1 & Data.Word == 1;
CarsCasesFemale1 = Data(CarsCasesFemaleIndex1, {'Agemos', 'measureToAnalyze'}); % Replace 'measureToAnalyze' with actual column name
PopCasesFemaleIndex1 = Data.Sex == 1 & Data.Word == 2;
PopCasesFemale1 = Data(PopCasesFemaleIndex1, {'Agemos', 'measureToAnalyze'});
Duck1CasesFemaleIndex1 = Data.Sex == 1 & Data.Word == 3;
Duck1CasesFemale1 = Data(Duck1CasesFemaleIndex1, {'Agemos', 'measureToAnalyze'});
Duck2CasesFemaleIndex1 = Data.Sex == 1 & Data.Word == 4;
Duck2CasesFemale1 = Data(Duck2CasesFemaleIndex1, {'Agemos', 'measureToAnalyze'});
VowelCasesFemaleIndex1 = Data.Sex == 1 & Data.Word == 5;
VowelCasesFemale1 = Data(VowelCasesFemaleIndex1, {'Agemos', 'measureToAnalyze'});
Once you have these tables, you can pass them to your `removeOutliersbyWords` function as you have shown in your example:
[CarsCases1, carscases1_outliersIndices] = removeOutliersbyWords(CarsCasesFemale1, 'measureToAnalyze', 'Agemos', 2.576);
% And so on for the other word categories...
Remember to replace `'measureToAnalyze'` with the actual name of the variable you are analyzing if it is not the correct column name.
I hope this helps!

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by