user3520120
user3520120

Reputation: 11

Matlab - count conditioning on previous value

I have a n x m matrix, and I have associated quantiles for each column (for example for m = 3)

-0.0627 -0.0564 -0.0413

How can I count the number of times Xn+1 descends below each associated quantile given that Xn has, for each column?

I could use sum ( X =< -0.0627) for the first column for example but how to model the condition that it should only count if the previous value was also below -0.0627?

I am sorry if the answer is too obvious or the question not clear but I am only starting with matlab.

Thanks!!!

Upvotes: 1

Views: 84

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112689

If I understand correctly:

data = [ -1 -1  0
         -2 -2  0
          0 -3 -1
         -2  0  0 ];
quant = [-0.0627 -0.0564 -0.0413];

R = bsxfun(@lt, data, quant);
result = sum(R(1:end-1,:) & R(2:end,:));

This gives the result [1 2 0] in my example.

Upvotes: 1

Brian
Brian

Reputation: 617

If X is a column, you can use & to join your two conditions that Xn and Xn+1 must be below -0.0627 like so:

Xn = X(1:end-1);
Xnplus1 = X(2:end);
sum(Xn <= -0.0627 &  Xnplus1 <= -0.0627)

Upvotes: 1

Related Questions