Zeros between Sign Change of Values in column

3 views (last 30 days)
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

Accepted Answer

Jan
Jan on 15 Apr 2021
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 Comments
Tommy Schumacher
Tommy Schumacher on 16 Apr 2021
whith this solution zeros are sign change too.
For Example -0.01, 0, -0.01 will make zero a sign change too!
Jan
Jan on 16 Apr 2021
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].

Sign in to comment.

More Answers (2)

Mathieu NOE
Mathieu NOE on 14 Apr 2021
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');
  1 Comment
Mathieu NOE
Mathieu NOE on 14 Apr 2021
Forgot to mention my result :
yy =
0
0
0
-0.0177
0
0.0075
0
-0.0170
0
0

Sign in to comment.


Tommy Schumacher
Tommy Schumacher on 14 Apr 2021
Edited: Tommy Schumacher on 14 Apr 2021
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 Comment
Mathieu NOE
Mathieu NOE on 15 Apr 2021
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!