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

A=[2 2 2 2 ] B=[3 3 3 3 ] want to check if A-B=k(ones) where k is an integer.

2 件のコメント

Stephen23
Stephen23 2018 年 9 月 7 日
"where k is an integer."
Can k be negative? Or zero? Or are only positive k allowed?
asim nadeem
asim nadeem 2018 年 9 月 7 日
K is non zero integer

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2018 年 9 月 7 日

0 投票

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 件のコメント

t = A - B;
if t(1) ~= 0 && t(1) == fix(t(1)) && all(diff(t) == 0)
passed
else
failed
end

サインインしてコメントする。

その他の回答 (1 件)

Alexander Jensen
Alexander Jensen 2018 年 9 月 6 日
編集済み: Alexander Jensen 2018 年 9 月 6 日
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 件のコメント

asim nadeem
asim nadeem 2018 年 9 月 7 日
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

サインインしてコメントする。

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by