Compacting the code by using an for loop or maybe an struct?

Hello,
I am an first year student working with Matlab (so little experience)
For an assignment i need to display the min, max and median of 4 1x25 array's (as you can see below)
The question is: Is there a way in which i can compact or change the code, by for example the usage of for-if statements or structs?

2 comentarios

Itwould be much easier to suggest a solution, if you post the code as text, such that we can reuse it by copy&paste.
Tygo Nikamp
Tygo Nikamp el 12 de Mzo. de 2021
Editada: Jan el 13 de Mzo. de 2021
I´m sorry, should´ve thought of that. Annyway, here´s the code:
%Loop settings
CLate = 0; %Empty variable used as a counter in a for loop for late movement
CEarly = 0; %Empty variable used as a counter in a for loop
for i = 1 : TotalParticipants
if Participant(i).GKMovement == 0 %If true the goalkeeper moved late
CLate = CLate + 1; %Counter + 1
GKLateKickDep(CLate) = Participant(i).KicksDependent; %Creates an array with the filtered data: Late GK movement and a dependent kick strategy
GKLateKickIndep(CLate) = Participant(i).KicksIndependent; %Creates an array with the filtered data: Late GK movement and a independent kick strategy
else %The GK didn't move late so what remains is early GK movement
CEarly = CEarly + 1; %Counter + 1
GKEarlyKickDep(CEarly) = Participant(i).KicksDependent; %Creates an array with the filtered data: Early GK movement and a dependent kick strategy
GKEarlyKickIndep(CEarly) = Participant(i).KicksIndependent; %Creates an array with the filtered data: Early GK movement and a independent kick strategy
end
end
%Calculating the min-max and mean values of each of the here above created categories to be displayed later on.
MaxGKLateKickDep = max(GKLateKickDep);
MinGKLateKickDep = min(GKLateKickDep);
MedianGKLateKickDep = median(GKLateKickDep);
MaxGKLateKickIndep = max(GKLateKickIndep);
MinGKLateKickIndep = min(GKLateKickIndep);
MedianGKLateKickIndep = median(GKLateKickIndep);
MaxGKEarlyKickDep = max(GKEarlyKickDep);
MinGKEarlyKickDep = min(GKEarlyKickDep);
MedianGKEarlyKickDep = median(GKEarlyKickDep);
MaxGKEarlyKickIndep = max(GKEarlyKickIndep);
MinGKEarlyKickIndep = min(GKEarlyKickIndep);
MedianGKEarlyKickIndep = median(GKEarlyKickIndep);

Iniciar sesión para comentar.

Respuestas (2)

Jan
Jan el 12 de Mzo. de 2021
You do not need the initial loop. Easier:
moved = ([Participant.GKMovement] == 0);
GKLateKickDep = Participant(moved).KicksDependent;
GKLateKickIndep = Participant(moved).KicksIndependent;
GKEarlyKickDep = Participant(~moved).KicksDependent;
GKEarlyKickIndep = Participant(~moved).KicksIndependent;
But a main problem is still the pile of variables, which care important information in the name. It is better to store data in the values.
GK(1).moved = true;
GK(1).Dependent = true;
GK(1).Value = Participant(moved).KicksDependent;
... etc.
Then you can run a loop over the elements of GK, which calculates the statistics.

8 comentarios

Thanks a lot!
I have been trying it out for a few hours but i just cant get ik working. The first pat that is supposed to replace the loop only delivers me the first of 25 values i need, I have tried looping it but it would only work for the GKLateKickDep & GKLateKickIndep.
Loop code:
for i = 1: 25
moved = ([Participant.GKMovement] == 0);
GKLateKickDep(i) = Participant(moved+i).KicksDependent
GKLateKickIndep(i) = Participant(moved+i).KicksIndependent
GKEarlyKickDep(i) = Participant(~moved+i).KicksDependent
GKEarlyKickIndep(i) = Participant(~moved+i).KicksIndependent
end
I have added a picture of the excel sheet to try to explain which data i want splitted. The data is later on boxplotted so i need it in an array/ 1x25 double (at least i think)
Thanks in advance
No, you do not need a loop anymore. [Participant.GKMovement] == 0 is a logical index already. Using this you can omit the loop.
Could you explain how? Because i tried the logical loop and it doesn't create the arrays/data i need for the boxplot or for calculating min/max/median.
I cannot know, which data you need for your boxplot.
This piece of code:
moved = ([Participant.GKMovement] == 0);
GKLateKickDep = Participant(moved).KicksDependent;
GKLateKickIndep = Participant(moved).KicksIndependent;
GKEarlyKickDep = Participant(~moved).KicksDependent;
GKEarlyKickIndep = Participant(~moved).KicksIndependent;
can replace your loop:
%Loop settings
CLate = 0; %Empty variable used as a counter in a for loop for late movement
CEarly = 0; %Empty variable used as a counter in a for loop
for i = 1 : TotalParticipants
if Participant(i).GKMovement == 0 %If true the goalkeeper moved late
CLate = CLate + 1; %Counter + 1
GKLateKickDep(CLate) = Participant(i).KicksDependent; %Creates an array with the filtered data: Late GK movement and a dependent kick strategy
GKLateKickIndep(CLate) = Participant(i).KicksIndependent; %Creates an array with the filtered data: Late GK movement and a independent kick strategy
else %The GK didn't move late so what remains is early GK movement
CEarly = CEarly + 1; %Counter + 1
GKEarlyKickDep(CEarly) = Participant(i).KicksDependent; %Creates an array with the filtered data: Early GK movement and a dependent kick strategy
GKEarlyKickIndep(CEarly) = Participant(i).KicksIndependent; %Creates an array with the filtered data: Early GK movement and a independent kick strategy
end
end
But the problem remains, that important information are coded in the namesof the variables. This makes it much harder to process the variables in a loop. So prefer to create an array of variables and store the information e.g. in the fields of a struct:
DepStrList = {'KicksIndependent', 'KicksDependent'};
iGK = 0;
for moved = [false, true]
for dependent = [false, true]
DepField = DepStrList{dependent + 1};
iGK = iGK + 1;
GK(iGK).moved = moved;
GK(iGK).dep = dependent;
GK(iGK).value = Participant(moved).(DepField);
end
end
for k = 1:numnel(GK)
GK(k).min = min(GK(k).value);
GK(k).max = max(GK(k).value);
GK(k).median = median(GK(k).value);
end
Do you see it? Now you could add a lot of further conditions easily without getting confusing names like MedianGKEarlyKickIndep.
I think i understand what you are doing here, but i cant get it working. It gives an error on the GK(iGK).value = Participant(moved).(DepField); line were its supposed to store the IndependentKicks percentage value. Here is a picture of the data i am using maybe it will help you understand. Im sorry that i'm taking up your time but i just can't quite comprehend what you are doing. Thanks in advance again!
Your questions are welcome. If I do not find the time to answer, I do not answer.
You mention, that you get an error. So please post the error message.
The error message is: Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
Error in PenaltyAnalysisScript (line 54)
GK(iGK).value = Participant(moved).(DepField);

Iniciar sesión para comentar.

a=Participant.GKMovement==0;
GK{1}=Participant.KicksDependent(a);
GK{2}=Participant.KicksIndependent(a);
GK{3}=Participant.KicksDependent(~a);
GK{4}=Participant.KicksIndependent(~a);
for k=1:4
MaxMinMeanGK(k,:)=[max(GK{k},min(GK{k},mean(GK{k})))];
end

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 12 de Mzo. de 2021

Comentada:

el 13 de Mzo. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by