diff not working on a vector of values
2 views (last 30 days)
Show older comments
Is there a reason why I cant calculate the difference between each of these values (x1)
This is the code Im using
[x1,y1]=SpotFind_NXC(app,ax2); %my function to locate x,y coordinates of spots in an image
x1
class(x1)
xdiff=abs(diff(x1))
and the command window:
x1 =
98.00
97.68
97.75
98.32
221.00
220.99
221.32
221.32
344.00
343.99
344.33
344.25
ans =
'double'
Unrecognized function or variable 'diff'.
2nd Question, once it works, how can I group these into "similar values". so ideally I would want the median (or mean) of the 1st group (98.00, 97.68. 97.75, 98.32), and then the median of the 2s group around 221 and the 3rd group around 344.
Thanks
Jason
0 Comments
Accepted Answer
Cris LaPierre
on 24 Apr 2024
There is nothing about the code you have shared that would prohibit you from using diff. For some reason, it is not on your MATLAB path. You may need to reset your path using restoredefaultpath
x1 = [98.00 97.68 97.75 98.32 221.00 220.99 221.32 221.32 344.00 343.99 344.33 344.25]';
xdiff=abs(diff(x1))
Not sure what typical values will be. You could use discretize or histcounts to bin your data. Here's one approach assuming your data is sorted.
% use xdiff to determine
nbins = sum(xdiff>mean(xdiff))+1
[N,edges,bin] = histcounts(x1,nbins)
splitapply(@mean,x1,bin)
More Answers (1)
Matt J
on 24 Apr 2024
x=[ 98.00
97.68
97.75
98.32
221.00
220.99
221.32
221.32
344.00
343.99
344.33
344.25];
G=findgroups(round(x,-1));
Medians=splitapply(@median,x,G)
See Also
Categories
Find more on Get Started with MATLAB 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!