MATLAB Answers

Get difference between array values

49 views (last 30 days)
leonidas86
leonidas86 on 28 May 2018
Edited: dpb on 28 May 2018
Hello, I have an one column array (see below) with logical 0 and 1. I want to get the difference between each value to detect a jump from 0 to 1 and vice versa. I tried:
find(diff(test(:,1))>0.8)
but everytime I get an error named:
Subscript indices must either be real positive integers or logicals.
whos test:
Name Size Bytes Class Attributes
test 2420x1 2420 uint8
I also tried to convert test to logical or int but the problem still exists. Can someone help?

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 28 May 2018
Edited: Stephen Cobeldick on 28 May 2018
You have a variable named diff. Delete that variable.
You might like to check all of these:
which test -all
which diff -all
which find -all

  0 Comments

Sign in to comment.

More Answers (2)

leonidas86
leonidas86 on 28 May 2018
Okay thanks Stephen!!

  0 Comments

Sign in to comment.


dpb
dpb on 28 May 2018
Edited: dpb on 28 May 2018
You can't store the difference from 1 to 0 in an unsigned integer; is there a reason you can't just use default double for test? You must use a signed integer, however.
Also, there's no need for the (:,1) subscript expression on a vector, and since the values are integer logicals, there's no need for the >0.8.
ipos=find([0; diff(test)]==10; % positive locations; augment to keep count same as original length
ineg=find([0; diff(test)]==-10; % ditto for negative

  3 Comments

leonidas86
leonidas86 on 28 May 2018
With:
ipos=find([0 diff(test)]==1);
i get the following error
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Stephen Cobeldick
Stephen Cobeldick on 28 May 2018
If test is a column vector then you need to use a semi-colon:
[0;diff(test)]
dpb
dpb on 28 May 2018
Yeah, intended to fix and forgot...good catch.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!