Error using discretize: Bin edges must be a vector that is real, numeric or logical, and monotonically increasing.
7 views (last 30 days)
Show older comments
Hi,
I have the following code:
N = numel(newdata);
split_newdata = cell(1,N);
split_newdata_mean = cell(1,N);
for ii = 1:N
temp = reshape([newdata{ii}{:}],[],2);
idx = discretize(temp(:,1),[0 split_points_noID(ii,:) Inf]);
split_newdata{ii} = groupsummary(temp(:,2),idx,@(x){x});
split_newdata_mean{ii} = cellfun(@(x)mean(x,'omitnan'),split_newdata{ii});
end
But I continue to get the error:
Error using discretize
Bin edges must be a vector that is real, numeric or logical, and monotonically increasing.
Error in CRM_analysis (line 167)
idx = discretize(temp(:,1),[0 split_points_noID(ii,:) Inf]);
I have attached the two files here. Essentially I am trying to split the individual cells in newdata according to the cut-off points in the cows of split_points_noID. If that makes sense.
Thank you for your help!
** EDIT:
I get a really weirdly incomplete split_newdata (see file).
0 Comments
Accepted Answer
Voss
on 15 Dec 2022
load newdata
load split_points_noID
Not every row of split_points_noID is monotonically increasing, because there are some trailing zeros sometimes
disp(split_points_noID)
To fix the code to work for this situation, remove the zeros from each row of split_points_noID, using logical indexing or nonzeros, before you use it in discretize:
N = numel(newdata);
split_newdata = cell(1,N);
split_newdata_mean = cell(1,N);
for ii = 1:N
temp = reshape([newdata{ii}{:}],[],2);
idx = discretize(temp(:,1),[0 nonzeros(split_points_noID(ii,:)).' Inf]); % nonzeros
% idx = discretize(temp(:,1),[0 split_points_noID(ii,split_points_noID(ii,:) > 0) Inf]); % logical indexing
split_newdata{ii} = groupsummary(temp(:,2),idx,@(x){x});
split_newdata_mean{ii} = cellfun(@(x)mean(x,'omitnan'),split_newdata{ii});
end
4 Comments
Steven Lord
on 15 Dec 2022
If you encounter this problem again I would set an error breakpoint and run your code. When MATLAB stops at the breakpoint, ask:
issorted(split_points_noID(ii, :))
If that returns false, you need to investigate what's on row ii of split_points_noID.
More Answers (0)
See Also
Categories
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!