diff not working on a vector of values

조회 수: 2 (최근 30일)
Jason
Jason 2024년 4월 24일
댓글: Jason 2024년 4월 24일
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

채택된 답변

Cris LaPierre
Cris LaPierre 2024년 4월 24일
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))
xdiff = 11x1
0.3200 0.0700 0.5700 122.6800 0.0100 0.3300 0 122.6800 0.0100 0.3400
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
nbins = 3
[N,edges,bin] = histcounts(x1,nbins)
N = 1x3
4 4 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
edges = 1x4
80 170 260 350
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
bin = 12x1
1 1 1 1 2 2 2 2 3 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
splitapply(@mean,x1,bin)
ans = 3x1
97.9375 221.1575 344.1425
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

추가 답변 (1개)

Matt J
Matt J 2024년 4월 24일
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)
Medians = 3x1
97.8750 221.1600 344.1250
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  댓글 수: 1
Jason
Jason 2024년 4월 24일
Thankyou Matt, I love this findgroups approach. Is it possible to define a value that defines whether its ina group or not
Thanks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by