Categorizing range of data
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Neuromechanist
le 8 Jan 2024
Commenté : Dyuman Joshi
le 8 Jan 2024
Hello,
I am using vector coding to analyze coordination patterns during arm reaching, which the values are from 0 to 360. I've used an if statement to attempt to categorize each value along 1:99 normalized time points on variable phsangSE so that I can eventually get a frequency of each category in the new variable coupAng. The return in coupAng is <undefined> on all time points except the last one, which it correctly categorizes. I'm not sure where the issue is in the code, but loops aren't my strongest suit at the moment, so any help is great. Or, if there is a better way to categorize the data I'm all ears.
Thank you in advance!
coupAng = categorical(nan(1,99),1:4,["Proximal Segment Phase","In-Phase","Distal Segment Phase","Anti-Phase"]);%nan(1,99),1:4,
for i = length(phsangSE)
if phsangSE(i) < 22.5 || phsangSE(i) >=157.5 && phsangSE(i) < 202.5 || phsangSE(i) >=337.5 && phsangSE(i) <= 360
coupAng(i) = ["Proximal Segment Phase"];
elseif phsangSE(i) >= 22.5 && phsangSE(i) < 67.5 || phsangSE(i) >= 202.5 && phsangSE(i) < 247.5
coupAng(i) = ["In-Phase"];
elseif phsangSE(i) >= 67.5 && phsangSE(i) < 112.5 || phsangSE(i) >= 247.5 && phsangSE(i) < 292.5
coupAng(i) = ["Distal Segment Phase"];
elseif phsangSE(i) >=112.5 && phsangSE(i) < 157.5 || phsangSE(i) >=292.5 && phsangSE(i) < 337.5
coupAng(i) = ["Anti-Phase"];
end
end
0 commentaires
Réponse acceptée
Dyuman Joshi
le 8 Jan 2024
Because the for loop only iterates through the last element of phsangSE, thus the rest of the elements are undefined as that's how they are preallocated.
I assume that's a typo and you want to loop through all of the elements -
%As you are allocating string to each element, it is better to preallocate a string array
coupAng = strings(1, 99);
% vv
for i = 1:length(phsangSE)
if phsangSE(i) < 22.5 || phsangSE(i) >=157.5 && phsangSE(i) < 202.5 || phsangSE(i) >=337.5 && phsangSE(i) <= 360
coupAng(i) = "Proximal Segment Phase";
elseif phsangSE(i) >= 22.5 && phsangSE(i) < 67.5 || phsangSE(i) >= 202.5 && phsangSE(i) < 247.5
coupAng(i) = "In-Phase";
elseif phsangSE(i) >= 67.5 && phsangSE(i) < 112.5 || phsangSE(i) >= 247.5 && phsangSE(i) < 292.5
coupAng(i) = "Distal Segment Phase";
elseif phsangSE(i) >=112.5 && phsangSE(i) < 157.5 || phsangSE(i) >=292.5 && phsangSE(i) < 337.5
coupAng(i) = "Anti-Phase";
end
end
Remove the square brackets around scalar strings, as they are superfluous.
2 commentaires
Dyuman Joshi
le 8 Jan 2024
"but my use of the || operator returned all but the first 16 cells, which were empty."
That's weird. It works fine here for a linearly spaced data sample -
phsangSE = linspace(1, 360, 99);
disp(phsangSE)
coupAng = strings(1, 99);
% vv
for i = 1:length(phsangSE)
if phsangSE(i) < 22.5 || phsangSE(i) >=157.5 && phsangSE(i) < 202.5 || phsangSE(i) >=337.5 && phsangSE(i) <= 360
coupAng(i) = "Proximal Segment Phase";
elseif phsangSE(i) >= 22.5 && phsangSE(i) < 67.5 || phsangSE(i) >= 202.5 && phsangSE(i) < 247.5
coupAng(i) = "In-Phase";
elseif phsangSE(i) >= 67.5 && phsangSE(i) < 112.5 || phsangSE(i) >= 247.5 && phsangSE(i) < 292.5
coupAng(i) = "Distal Segment Phase";
elseif phsangSE(i) >=112.5 && phsangSE(i) < 157.5 || phsangSE(i) >=292.5 && phsangSE(i) < 337.5
coupAng(i) = "Anti-Phase";
end
end
disp(coupAng)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with MATLAB 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!