Creating a variable that denotes a range of values

1 view (last 30 days)
I have a dataset that looks like this:
Signal PT#1 PT#2 ... PT#26
0.20 2121 1232
0.21 1233 1233
0.22 1236 1232
0.23 1232 1236
...
1.00 1212 1232
...
4.00 1212 12321
I used this code to discretize the values to the hundredth's and average it for each group of values.
edges = (0:0.05:4);
allData(:,27) = discretize(allData(:,1),edges);
[C,~,IC] = unique(allData(:,27));
avg=NaN(76,26);
for i = 1:25
avg(:,i) = accumarray(IC, allData(:, i+1), [], @mean); % calculate the mean in your data of column 2 for rows in IC with identical elements
end
Result = [C avg];
This resulted in a dataset "Result" that looks like this
1 1241 1231 1232
2 1232 1232 1231
...
76 2312 1231 1231
How do I convert the first column into ranges of the variables that were discretized? Like I want the first row to say "0.2 to 0.25" rather than "1" ?

Accepted Answer

Steven Lord
Steven Lord on 27 Jun 2022
x = 4*rand(10, 1);
edges = (0:0.05:4);
y = discretize(x, edges, 'categorical');
results = table(x, y)
results = 10×2 table
x y _______ ___________ 3.161 [3.15, 3.2) 0.16367 [0.15, 0.2) 1.6123 [1.6, 1.65) 1.2792 [1.25, 1.3) 1.7895 [1.75, 1.8) 1.1378 [1.1, 1.15) 1.7702 [1.75, 1.8) 3.847 [3.8, 3.85) 1.756 [1.75, 1.8) 2.1521 [2.15, 2.2)
Or if you want more control over the format of the category names:
categoryNames = edges(1:end-1) + " to " + edges(2:end);
z = discretize(x, edges, 'categorical', categoryNames);
results2 = table(x, z)
results2 = 10×2 table
x z _______ ___________ 3.161 3.15 to 3.2 0.16367 0.15 to 0.2 1.6123 1.6 to 1.65 1.2792 1.25 to 1.3 1.7895 1.75 to 1.8 1.1378 1.1 to 1.15 1.7702 1.75 to 1.8 3.847 3.8 to 3.85 1.756 1.75 to 1.8 2.1521 2.15 to 2.2

More Answers (0)

Community Treasure Hunt

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

Start Hunting!