Categorizing range of data
14 ビュー (過去 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 で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!