Hello, I want to be able to put data into a bin but in a strange way.
My bin centres are 0, -2765, -5530, -8295, -11060
I only want data to slot into any of these bins if its within +/- 20 of the centre.
So for data to be in the 1st in it has to be in the range 20 to -20
And for the 2nd bin, in the range -2745 to -2785
Thnaks for any help

댓글 수: 2

the cyclist
the cyclist 2019년 9월 16일
What do you want the output to be, specifically?
Jason
Jason 2019년 9월 16일
Any data to be one of those values if within the bin width.
So
10 -> 0
200->NaN
-2760 -> -2765
-2770 -> -2765
-2780 -> -2765
-2790 -> NaN

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

 채택된 답변

the cyclist
the cyclist 2019년 9월 16일
편집: the cyclist 2019년 9월 17일

0 개 추천

% Simulate some data
data = [0; 0; -10; -2766; -5600; -5535];
% Define bin centers
binCenter = [ 0, -2765, -5530, -8295, -11060];
% Find distance between each point and each bin center
% (Relies on implicit expansion)
distance = abs(data - binCenter);
% Find distance to nearest bin center (and index of that center)
[minDist,minIdx] = min(distance,[],2);
% New variable with value of nearest bin center (or NaN if too far away)
data2 = binCenter(minIdx)';
data2(minDist > 20) = NaN;

댓글 수: 3

Jason
Jason 2019년 9월 17일
Hello, thanks for your help.
This is what I get as a result of
[data,data2]
ans =
0 0
-0.10 0
-0.10 0
-0.10 0
0 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
0 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-0.10 0
-2765.20 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-2765.10 0
-5530.10 0
-5530.00 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.00 0
-5530.00 0
-5530.10 0
-5530.00 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
-5530.10 0
Hm. That's not what I get. I made a correction after I posted, so maybe you caught the code before that? I made another couple changes above (smaller test set, and oriented data2 to be a column vector). Try that.
I get
[data, data2]
ans =
0 0
0 0
-10 0
-2766 -2765
-5600 NaN
-5535 -5530
Jason
Jason 2019년 9월 17일
Yes that worked, thankyou very much.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 NaNs에 대해 자세히 알아보기

태그

질문:

2019년 9월 16일

댓글:

2019년 9월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by