Change value based on the values of another column
조회 수: 36 (최근 30일)
이전 댓글 표시
I have a table with 2 columns and 6000 rows each. Sample values in the 1st column is like:
-0.00554
-0.00503
-0.00406
-0.00406
-0.00316
-0.00274
-0.00274
0
0
0
0
0
0
0.00233
0.00452
0.00552
0.00715
0.00831
0
0
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
0
0
0
0.00552
0.00715
0.00831
0.00715
0.00831
0
0
0
0
0
-0.00406
-0.00316
The corresponding values in the 2nd column will be starting from 1,2,3, etc. until the positive value changes again to negative value in the 1st column. To be more specific, 1st column will be having values starting with -ve sign followed by 0s and +ve values. So, the -ve value will be changing to +ve and again back to -ve. Once the -ve value show up again, the count on the 2nd column should start from 1 again. Please help me wih this.
댓글 수: 0
채택된 답변
Star Strider
2022년 8월 5일
Another approach —
v = [-0.00554
-0.00503
-0.00406
-0.00406
-0.00316
-0.00274
-0.00274
0
0
0
0
0
0
0.00233
0.00452
0.00552
0.00715
0.00831
0
0
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
0
0
0
0.00552
0.00715
0.00831
0.00715
0.00831
0
0
0
0
0
-0.00406
-0.00316];
sv = sign(v);
sv(sv>=0) = 1; % Adjust 'sign' Vector
pn = [0 strfind(sv.', [1 -1]) numel(v)]; % +vn To -ve Transition Indices
for k = 1:numel(pn)-1
idx = [pn(k) pn(k+1)-1]+1; % Index Range
Col2(idx(1):idx(2)) = (idx(1) : idx(2)) - (idx(1)-1); % Create Column #2 (As Row Vector)
end
Result = [v, Col2(:)] % Full Matrix
ResultMtx = [Result(1:10,:) Result(11:20,:) Result(21:30,:) Result(31:40,:)] % Display (Remove Later)
ResultEnd = Result(41:end,:) % Display (Remove Later)
.
댓글 수: 10
Star Strider
2022년 8월 8일
The values for ‘Input1’ are different, and I do not understand how either it or ‘Output’ are incremented.
I just do not see any sort of pattern here that lends itself to being coded.
추가 답변 (2개)
dpb
2022년 8월 5일
편집: dpb
2022년 8월 5일
If there's always at least one zero before the new -ive value excepting the initial element, then
ix=find(diff(sign([0;x]))==-1);
will locate the beginng line of each section.
Or, actually, on reflection,
ix=find(diff(sign([0;x]))<0);
will find a transition from either 0 to -ive or +ive to -ive
Andrei Bobrov
2022년 8월 5일
v = [-0.00554
-0.00503
-0.00406
-0.00406
-0.00316
-0.00274
-0.00274
0
0
0
0
0
0
0.00233
0.00452
0.00552
0.00715
0.00831
0
0
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
-9.00000000000000e-05
0
0
0
0.00552
0.00715
0.00831
0.00715
0.00831
0
0
0
0
0
-0.00406
-0.00316];
s = sign(v);
p = diff([0;s]) == -1 & s == -1;
i = accumarray(cumsum(p),1);
x = ones(size(p));
x(p) = x(p) - [ 0;i(1:end-1)];
out = cumsum(x);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!