Zeros between Sign Change of Values in column

1 次查看(过去 30 天)
Hello,
i have a matrix A (1080x2). In the 2. Column i have values which contains sign changes
1.Column 2. Column
1 0
2 0
3 0.00047
4 -0.0177
5 -0.0328
6 0.0075
7 0
8 -0.0170
9 0
10 0
The values between sign changes in 2. column should be zero and keep the first value of sign change
1.Column 2. Column
1 0
2 0
3 0
4 -0.0177
5 0
6 0.0075
7 0
8 -0.0170
9 0
10 0
Maybe helpful is this Question/Answer:
Thank You

采纳的回答

Jan
Jan 2021-4-15
data = [ ...
1 0; ...
2 0; ...
3 0.00047; ...
4 -0.0177; ...
5 -0.0328; ...
6 0.0075; ...
7 0; ...
8 -0.0170; ...
9 0; ...
10 0];
idx = [true; diff(data(:, 2) >= 0) == 0];
data(idx, 2) = 0
data = 10×2
1.0000 0 2.0000 0 3.0000 0 4.0000 -0.0177 5.0000 0 6.0000 0.0075 7.0000 0 8.0000 -0.0170 9.0000 0 10.0000 0
  2 个评论
Tommy Schumacher
Tommy Schumacher 2021-4-16
whith this solution zeros are sign change too.
For Example -0.01, 0, -0.01 will make zero a sign change too!
Jan
Jan 2021-4-16
Does this comment mean, that you want to treat the zero in another way?
Then please post an exhaustive set of input data to define uniquely, what you want to achieve. What is the wanted output for:
[-2, -1, 0, -1, -2]
[-2, -1, 0, 1, -2]
[1, 1, 0, 1, 1]
[1, 1, 0, -1, -1]
It is not automatically clear, how you want to treat the zeros or where the "sign change" occurs in [-1, 0, 1].

请先登录,再进行评论。

更多回答(2 个)

Mathieu NOE
Mathieu NOE 2021-4-14
hello
this is my suggestion below + see attachements
data = importdata('data.txt');
x = data(:,1);
y = data(:,2);
threshold = 0; %
[t0_pos,s0_pos,t0_neg,s0_neg]= crossing_V7(y,x,threshold,'linear'); % positive (pos) and negative (neg) slope crossing points
% t0 => corresponding time (x) values
% s0 => corresponding function (y) values , obviously they must be equal to "threshold"
figure(1)
plot(x,y,t0_pos,s0_pos,'+r',t0_neg,s0_neg,'+g','linewidth',2,'markersize',12);grid on
legend('signal','positive slope crossing points','negative slope crossing points');
% now let's force y values to be zero when x values are one step before positive and negative slope zero crossing points
dx = mean(diff(x)); % x axis spacing
indp_before= floor(t0_pos/dx); % index of values before positive slope zero crossing points
indn_before= floor(t0_neg/dx); % index of values before negative slope zero crossing points
yy = y;
yy(indp_before)= 0;
yy(indn_before)= 0;
figure(2)
plot(x,y,x,yy,t0_pos,s0_pos,'+r',t0_neg,s0_neg,'+g','linewidth',2,'markersize',12);grid on
legend('signal','signal modified','positive slope crossing points','negative slope crossing points');

Tommy Schumacher
Tommy Schumacher 2021-4-14
编辑:Tommy Schumacher 2021-4-14
thanks. For the test values above its working, but for my real working variable (see data.mat attached) is not working (it zeros the found sign change value). You know why?
Thank You!
  1 个评论
Mathieu NOE
Mathieu NOE 2021-4-15
hmm , I am not sure to understand the issue
I exported the original (first column) and modified signal (second column) in the attached excel file
can you add a 3rd column with the expected results ?
thanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 NaNs 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by