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 个评论
Jan
2021-3-12
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
2021-3-12
编辑:Jan
2021-3-13
回答(2 个)
Jan
2021-3-12
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 个评论
Tygo Nikamp
2021-3-12
Tygo Nikamp
2021-3-13
Jan
2021-3-13
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
2021-3-13
Jan
2021-3-13
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
2021-3-13
Jan
2021-3-13
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
2021-3-13
David Hill
2021-3-12
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
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

