How to count how often a variable returns to zero and then increases from zero
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi everyone, imagine you have a table of Time and a Y variable, could be anything, current, voltage, torque etc. What would be the best way to code MATLAB to count how often the value goes down to zero and then increases above zero again? E.g.
Time Voltage
_____ _____
1 3
2 2
3 1
4 0
5 1
6 0
7 2
8 3
9 0
10 3
11 3
12 2
13 1
14 0
15 1
16 0
17 2
18 3
19 0
So you can see sometimes the voltage starts to rise, and then it drops, and eventually hits 0, and then starts to rise, and then starts to fall, and then hits zero etc.I want some kind of counter that would increase every time the value drops to zero (or some threshold) and then increases above the threshold again.
Antworten (2)
John D'Errico
am 21 Feb. 2024
Bearbeitet: John D'Errico
am 21 Feb. 2024
It appears this parameter never goes negative.
Are your sets ALWAYS such that a zero is always alone? So when it hits zero, it never stays there?
If this is true, then just count the number of zeros. If a zero as the very first or last element does not count, then subtract 1 if the first element is a zero. Subtract 1 if the last element is a zero.
It seems trivial. If there are cases where the variable stays at zero, then just subtract 1 from the count whenever a zero is followed by another zero.
Again, all of this is trivial. As far as being in a table, just extract that variable from the table. Tables are not the only variable type in MATLAB, and tables often make your work far more difficult. So for the computation, just extract that variable as a vector. LEARN TO USE MATLAB. If all you ever do is use tables, you are just using MATLAB as a pseudo-spreadsheet. For example:
V = randi([0,3],[1,20])
Zcount = sum(V == 0) % how many zeros?
Zcount = Zcount - (V(1) == 0) - (V(end) == 0) % Discard a zero at an end
Next, we need to discard multiple consecutive zeros in that count, But I assume that a sequence like [1 0 0 1] still counts as one event when it hit zero. How can we do that? Just search for any pairs of consecutive zeros.
Zcount = Zcount - numel(strfind(V,[0 0]))
So if we had a string like [1 0 0 0 1], we would find TWO consecutive pairs of zeros, but still one event. In fact, in the random string you see above, there was an initial zero. I discarded that from the count. Then there was a tripleton zero, so I cut the count by 2. In the end, there were exactly 6 events of the class you want to see.
I think this is what you want to see, but you should understand from my explanations if some of those tests are not what you wanted.
When you don't know how to solve a problem, start by finding something that might get you close to a solution. Can you then modify that? Make it better? Get you closer to a solution. Many problems in MATLAB can be resolved this way.
2 Kommentare
Voss
am 22 Feb. 2024
"The data also contains millions of data points. You can't just count the zeros."
Sure you can.
% a vector of a billion elements, each of which is zero or one:
v = randi([0,1],1e9,1);
% count the zeros and time how long it takes to do so:
t = tic();
n_zeros = nnz(v == 0);
dt = toc(t);
% display the number of zeros and the time it took to count them:
disp(sprintf('It took %g seconds to count %d zeros.',dt,n_zeros))
Did you try the answer?
Star Strider
am 21 Feb. 2024
One method to find the first instance of a zero value is to use the strfind function. You can use it again to determine the indices of a return from a zero value, then use those results to determine the start and end indices of the non-zero values. If you want the non-zero values themselves, use accumarray to retrieve them, then if necessary, edit the results —
A = [ 1 3
2 2
3 1
4 0
5 1
6 0
7 2
8 3
9 0
10 0
11 0
12 3
13 3
14 2
15 1
16 0
17 1
18 0
19 2
20 3
21 0];
TV = array2table(A, 'VariableNames',{'Time','Voltage'})
Vv = TV.Voltage ~= 0
zero_start = strfind(Vv.', [1 0])+1
zero_end = strfind(Vv.', [0 1])
nz = cumsum(TV.Voltage == 0) + 1;
TVNZ = accumarray(nz, (1:numel(nz)).', [], @(x){TV(x(TV{x,2}~=0),:)})
Lv = cellfun(@(x)~isempty(x),TVNZ);
Result = TVNZ(Lv);
Result{:}
I’m not certain what result you want, however one of these should provide an approach to it.
.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!