Creating a variable that denotes a range of values
20 次查看(过去 30 天)
显示 更早的评论
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" ?
0 个评论
采纳的回答
Steven Lord
2022-6-27
x = 4*rand(10, 1);
edges = (0:0.05:4);
y = discretize(x, edges, 'categorical');
results = table(x, y)
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!