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

采纳的回答

Dyuman Joshi
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 个评论
Neuromechanist
Neuromechanist 2024-1-8
Thanks,! I appreciate your patience. The typo without the 1: was a face palm. Your answer worked great, but my use of the || operator returned all but the first 16 cells, which were empty . I removed the || operator and created a new elseif statement with that range and now all cells are categorized.
Dyuman Joshi
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)
Columns 1 through 19 1.0000 4.6633 8.3265 11.9898 15.6531 19.3163 22.9796 26.6429 30.3061 33.9694 37.6327 41.2959 44.9592 48.6224 52.2857 55.9490 59.6122 63.2755 66.9388 Columns 20 through 38 70.6020 74.2653 77.9286 81.5918 85.2551 88.9184 92.5816 96.2449 99.9082 103.5714 107.2347 110.8980 114.5612 118.2245 121.8878 125.5510 129.2143 132.8776 136.5408 Columns 39 through 57 140.2041 143.8673 147.5306 151.1939 154.8571 158.5204 162.1837 165.8469 169.5102 173.1735 176.8367 180.5000 184.1633 187.8265 191.4898 195.1531 198.8163 202.4796 206.1429 Columns 58 through 76 209.8061 213.4694 217.1327 220.7959 224.4592 228.1224 231.7857 235.4490 239.1122 242.7755 246.4388 250.1020 253.7653 257.4286 261.0918 264.7551 268.4184 272.0816 275.7449 Columns 77 through 95 279.4082 283.0714 286.7347 290.3980 294.0612 297.7245 301.3878 305.0510 308.7143 312.3776 316.0408 319.7041 323.3673 327.0306 330.6939 334.3571 338.0204 341.6837 345.3469 Columns 96 through 99 349.0102 352.6735 356.3367 360.0000
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)
Columns 1 through 9 "Proximal Segment P…" "Proximal Segment P…" "Proximal Segment P…" "Proximal Segment P…" "Proximal Segment P…" "Proximal Segment P…" "In-Phase" "In-Phase" "In-Phase" Columns 10 through 21 "In-Phase" "In-Phase" "In-Phase" "In-Phase" "In-Phase" "In-Phase" "In-Phase" "In-Phase" "In-Phase" "In-Phase" "Distal Segment Pha…" "Distal Segment Pha…" Columns 22 through 28 "Distal Segment Pha…" "Distal Segment Pha…" "Distal Segment Pha…" "Distal Segment Pha…" "Distal Segment Pha…" "Distal Segment Pha…" "Distal Segment Pha…" Columns 29 through 38 "Distal Segment Pha…" "Distal Segment Pha…" "Distal Segment Pha…" "Anti-Phase" "Anti-Phase" "Anti-Phase" "Anti-Phase" "Anti-Phase" "Anti-Phase" "Anti-Phase" Columns 39 through 47 "Anti-Phase" "Anti-Phase" "Anti-Phase" "Anti-Phase" "Anti-Phase" "Proximal Segment P…" "Proximal Segment P…" "Proximal Segment P…" "Proximal Segment P…" Columns 48 through 54 "Proximal Segment P…" "Proximal Segment P…" "Proximal Segment P…" "Proximal Segment P…" "Proximal Segment P…" "Proximal Segment P…" "Proximal Segment P…" Columns 55 through 66 "Proximal Segment P…" "Proximal Segment P…" "In-Phase" "In-Phase" "In-Phase" "In-Phase" "In-Phase" "In-Phase" "In-Phase" "In-Phase" "In-Phase" "In-Phase" Columns 67 through 74 "In-Phase" "In-Phase" "Distal Segment Pha…" "Distal Segment Pha…" "Distal Segment Pha…" "Distal Segment Pha…" "Distal Segment Pha…" "Distal Segment Pha…" Columns 75 through 83 "Distal Segment Pha…" "Distal Segment Pha…" "Distal Segment Pha…" "Distal Segment Pha…" "Distal Segment Pha…" "Distal Segment Pha…" "Anti-Phase" "Anti-Phase" "Anti-Phase" Columns 84 through 94 "Anti-Phase" "Anti-Phase" "Anti-Phase" "Anti-Phase" "Anti-Phase" "Anti-Phase" "Anti-Phase" "Anti-Phase" "Anti-Phase" "Proximal Segment P…" "Proximal Segment P…" Columns 95 through 99 "Proximal Segment P…" "Proximal Segment P…" "Proximal Segment P…" "Proximal Segment P…" "Proximal Segment P…"

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by