Info
This question is closed. Reopen it to edit or answer.
How to use firs value from array column when threshold and others change to zero, moreover if zero repeated for number of times change that element value?
1 view (last 30 days)
Show older comments
Hello Everyone, I have the following code, which was provided by @Stephen Cobeldick, that converts array D
D= -2 2 3 -1 -5
1 4 7 3 4
5 3 -4 6 -5
-7 -2 -1 4 -1
To array B.
B=0 2 3 0 0
1 0 0 3 4
0 0 0 0 -5
-7 -2 -1 0 0
0 2 0 -5 0
Here is the code for that:
X= D>thr;
Y = D<thr & cumsum(X)>0;
Z = [X(1,:); diff(X)>0 | diff(Y)>0];
B = zeros(size(D));
B(Z) = D(Z)
In short this algorithm takes first value when threshold is reached and others are filled with zero. Now I would like to modify B array, that if zeros is repeated two times in a row, it would be change to -1 and the next value which is above or below threshold is changed to zero that result N array would look and the structure should be the same as in original array B that there can not be like this 1 0 0 -1 0 -1, it should be 1 0 0 -1 0 0:
N=0 2 3 0 0
1 0 0 3 4
0 -1 -1 0 -5
-7 0 0 0 0
0 2 0 -5 0
I have the following code that counts how many time value occurs in a row, but it just a bit of what I need:
x = [4 4 4 1 1 1 5 5 5 0 0 0 7 7 7 1 1 1 1];
i = find(diff(x)) ;
n = [i numel(x)] - [0 i];
c = arrayfun(@(X) X-1:-1:0, n , 'un',0);
d=cellfun(@(x) fliplr(x),c,'un',0);
y = cat(2,c{:});
0 Comments
Answers (0)
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!