Compacting the code by using an for loop or maybe an struct?
Mostrar comentarios más antiguos
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
Jan
el 12 de Mzo. de 2021
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
el 12 de Mzo. de 2021
Editada: Jan
el 13 de Mzo. de 2021
Respuestas (2)
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
Tygo Nikamp
el 12 de Mzo. de 2021
Tygo Nikamp
el 13 de Mzo. de 2021
Jan
el 13 de Mzo. de 2021
No, you do not need a loop anymore. [Participant.GKMovement] == 0 is a logical index already. Using this you can omit the loop.
Tygo Nikamp
el 13 de Mzo. de 2021
Jan
el 13 de Mzo. de 2021
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.
Tygo Nikamp
el 13 de Mzo. de 2021
Jan
el 13 de Mzo. de 2021
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.
Tygo Nikamp
el 13 de Mzo. de 2021
David Hill
el 12 de Mzo. de 2021
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
1 comentario
Tygo Nikamp
el 12 de Mzo. de 2021
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

