MATLAB Answers

# how to check that difference of two vectors is a multiple of ones in matlab

4 views (last 30 days)
asim nadeem on 6 Sep 2018
Commented: Walter Roberson on 7 Sep 2018
A=[2 2 2 2 ] B=[3 3 3 3 ] want to check if A-B=k(ones) where k is an integer.
##### 2 CommentsShowHide 1 older comment
asim nadeem on 7 Sep 2018
K is non zero integer

Sign in to comment.

### Accepted Answer

Walter Roberson on 7 Sep 2018
Check that the first entry of A-B is an integer with mod() or by comparing it to fix() of itself. Then check that diff() of A-B is all zero.
##### 1 CommentShowHide None
Walter Roberson on 7 Sep 2018
t = A - B;
if t(1) ~= 0 && t(1) == fix(t(1)) && all(diff(t) == 0)
passed
else
failed
end

Sign in to comment.

### More Answers (1)

Alexander Jensen on 6 Sep 2018
Edited: Alexander Jensen on 6 Sep 2018
Is this what you're looking for?:
isInt = ~logical(mod(A-B,1))
isInt =
1×4 logical array
0 1 1 1
The logical(X) function returns everything that is not 0 as TRUE.
##### 1 CommentShowHide None
asim nadeem on 7 Sep 2018
if A= [2 6], B=[6 2] then A-B=[-4 4] so A-B is not an integer multiple of ones as if we take 4 common we get 4*[-1 1]. how can I check it for arbitrary A and B. so if it is multiple of ones then I need 0 or 1 single answer

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!