How to find multiples of x in a row vector?

4 ビュー (過去 30 日間)
Kyle Gray
Kyle Gray 2016 年 2 月 2 日
コメント済み: Guillaume 2016 年 2 月 2 日
I searched the forms and didn't find any pertinent information on finding multiples of a specific number or set of numbers in a row vector. Ex given x=[1:100] how would I find a= multiples of 3 or combining two functions and find b = multiples of 3 but not multiples of 2. c = multiples of 3 or multiples of 2. etc.
I tried using the find function as a=find mult(x,3). However that doesnt work. Any suggestions would be much appreciated.

採用された回答

Guillaume
Guillaume 2016 年 2 月 2 日
First of all, it's entirely possible you don't need the find function. Unless you actually need the indices of the element that match your condition, you're better off simply using logical indexing.
Secondly, to find something that matches multiple conditions, you simply use logical operators to combine theses conditions.
Finally, to find if a number is a multiple of another, you simply divide the former by the latter and see if the remainder is 0. The function to get the remainder is rem ( mod also works).
So, to get the elements of x that are multiple of 3 AND NOT multiple of 2:
x(rem(x, 3) == 0 && rem(x, 2) ~= 0)
  4 件のコメント
Walter Roberson
Walter Roberson 2016 年 2 月 2 日
x(rem(x, 3) == 0 & rem(x, 2) ~= 0)
Guillaume
Guillaume 2016 年 2 月 2 日
Oh yes, sorry a silly typo on my part. It should have read & not &&.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by