What's the best way to shorten this?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
This is a very lengthy code for something repetitive, I want to know whether there is a simpler, more efficient way of doing this.
The purpose of the code is to load text files and trim them according to the same criteria and check which one is the shortest and use the shortest length in calculations that happen later in the code.
T1 = dlmread("Trial 1");
T2 = dlmread("Trial 2");
T3 = dlmread("Trial 3");
T4 = dlmread("Trial 4");
T5 = dlmread("Trial 5");
T6 = dlmread("Trial 6");
T7 = dlmread("Trial 7");
T8 = dlmread("Trial 8");
T9 = dlmread("Trial 9");
T10 = dlmread("Trial 10");
%removal of unwanted elements from the raw data array
T1(T1(:,11)<=0 ,:) = [];
T1(T1(:,3)==25 ,:) = [];
T1(T1(:,3)<500 ,:) = [];
T1(T1(:,5)==0 ,:)= [];
T1(find(T1(:,12)>780,1):end, :) = [];
T2(T2(:,11)<=0 ,:) = [];
T2(T2(:,3)==25 ,:) = [];
T2(T2(:,3)<500 ,:) = [];
T2(T2(:,5)==0 ,:)= [];
T2(find(T2(:,12)>780,1):end, :) = [];
T2(T2(:,11)<=0 ,:) = [];
T3(T3(:,3)==25 ,:) = [];
T3(T3(:,3)<500 ,:) = [];
T3(T3(:,5)==0 ,:)= [];
T3(find(T3(:,12)>780,1):end, :) = [];
T4(T4(:,11)<=0 ,:) = [];
T4(T4(:,3)==25 ,:) = [];
T4(T4(:,3)<500 ,:) = [];
T4(T4(:,5)==0 ,:)= [];
T4(find(T4(:,12)>780,1):end, :) = [];
T5(T5(:,11)<=0 ,:) = [];
T5(T5(:,3)==25 ,:) = [];
T5(T5(:,3)<500 ,:) = [];
T5(T5(:,5)==0 ,:)= [];
T5(find(T5(:,12)>780,1):end, :) = [];
T6(T6(:,11)<=0 ,:) = [];
T6(T6(:,3)==25 ,:) = [];
T6(T6(:,3)<500 ,:) = [];
T6(T6(:,5)==0 ,:)= [];
T6(find(T6(:,12)>780,1):end, :) = [];
T7(T7(:,11)<=0 ,:) = [];
T7(T7(:,3)==25 ,:) = [];
T7(T7(:,3)<500 ,:) = [];
T7(T7(:,5)==0 ,:)= [];
T7(find(T5(:,12)>780,1):end, :) = [];
T8(T8(:,11)<=0 ,:) = [];
T8(T8(:,3)==25 ,:) = [];
T8(T8(:,3)<500 ,:) = [];
T8(T8(:,5)==0 ,:)= [];
T8(find(T8(:,12)>780,1):end, :) = [];
T9(T9(:,11)<=0 ,:) = [];
T9(T9(:,3)==25 ,:) = [];
T9(T9(:,3)<500 ,:) = [];
T9(T9(:,5)==0 ,:)= [];
T9(find(T9(:,12)>780,1):end, :) = [];
T10(T10(:,11)<=0 ,:) = [];
T10(T10(:,3)==25 ,:) = [];
T10(T10(:,3)<500 ,:) = [];
T10(T10(:,5)==0 ,:)= [];
T10(find(T10(:,12)>780,1):end, :) = [];
L1=length(T1);
L2=length(T2);
L3=length(T3);
L4=length(T4);
L5=length(T5);
L6=length(T6);
L7=length(T7);
L8=length(T8);
L9=length(T9);
L10=length(T10);
Lmin = min([L1(:);L2(:);L3(:);L4(:);L5(:);L6(:);L7(:);L8(:);L9(:);L10(:)]);
3 commentaires
Stephen23
le 13 Mai 2019
Using numbered variables is a sign that you are doing something wrong.
Copy-and-pasting code is a sign that you are doing something wrong.
Remember that computers are only really good at one thing: repeatedly doing simple tasks. So when you find yourself copy-and-pasting the same code over and over again, then you are just doing the computer's job for it. Instead get the computer to do it for you: use a loop.
Réponse acceptée
Adam Danz
le 9 Mai 2019
Modifié(e) : Adam Danz
le 13 Mai 2019
This should get you started but it's not tested and there will likely be some kinks to work out. I'd be glad to help more if needed.
trials = ["Trial 1", "Trial 2", "Trial 3", "Trial 4", "Trial 5", ...
"Trial 6", "Trial 7", "Trial 8", "Trial 9", "Trial 10" ];
nTrials = numel(trials);
T = cell(size(trials));
for i = 1:nTrials
T{i} = dlmread(trials(i));
%removal of unwanted elements from the raw data array
T{i}(T{i}(:,11)<=0 ,:) = [];
T{i}(T{i}(:,3)<500 ,:) = [];
T{i}(T{i}(:,5)==0 ,:)= [];
T{i}(find(T{i}(:,12)>780,1):end, :) = [];
end
L = cellfun(@length, T,'UniformOutput', false);
Lmin = min([L{:}]);
2 commentaires
Adam Danz
le 13 Mai 2019
The first input to cellfun() is a function handle. The second input is a cell array. The cellfun() function applies the function in input #1 to each element of the cell array in input #2. For example, the cell array T (defined below) has 4 elements. The length of the four elements are [3, 4, 5, 2].
T = { [1 2 3], [1 2 3 4], [1 2 3 4 5], [1 2] };
The 'UniformOutput' flag specifies that the output should be stored in a cell array.
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements 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!