Steven Goddard
Steven Goddard

Reputation: 79

Matlab Syntax Confusion

I have the following Matlab syntax and I don't understand what it is doing, particularly the xDiff == 2 part which looks like a weird way to reference a specific value in the tDiff array. Can anyone advise?

tTacho = tDiff(xDiff == 2);

tDiff and xDiff are 479999x1 arrays

Thanks

Upvotes: 0

Views: 57

Answers (1)

dcts
dcts

Reputation: 1639

The first operation xDiff==2 returns a logical array which gives you the information, which positions in xDiff are equal to 2. Then it applies this positions to the second array yDiff. Maybe this short example hepls (simplified):

xDiff   = [0 0 2 2 0 0 0 0 2];    % simplified: only values 0 or 2
yDiff   = [1 2 3 4 5 6 7 8 9];   
tTacho  = [    3 4         9];
tTacho2 = yDiff(xDiff==2);

In this example tTacho and tTacho2 are the same. What we are basically doing: the 3rd, 4th and 9th position in xDiff are equal to 2, so we take the values of the 3rd, 4th and 9th position of yDiff and store them in the new array tTacho. Be aware that the length of tTacho is dependent on how many values in xDiff equal to 2 (in this example case there are 3, hence we get a 3x1 array).

The arrays xDiff and yDiff seem to be related in some way, at least from a logical point of view they should be. I hope this helps!

Upvotes: 3

Related Questions