Categorizing range of data
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Dyuman Joshi
2024-1-8
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 个评论
Dyuman Joshi
2024-1-8
"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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!