Removing Multiples of a number from a Matrix

4 views (last 30 days)
Hello all, I am very new to matlab as I am learning it for a lab i joined in school. Currently my job is only to learn matlab, functions, basics etc....
I was wondering if someone can tell me how to Remove multiples of a number from a given matrix.
For example:
Suppose i have a matrix that is
n = 1:1:20
I want to remove all the multiples of 2 from this matrix but NOT 2 such that when i displayed this "new" matrix without multiples of 2, matrix "m", it would return:
disp(m)
m = 1 2 3 5 7 9 11 13 15 17 19
would someone show me how i can do this? and also if there is a better approach to what i am doing? I want to avoid using plugins or anything, this is completely a learning experience and not something I will need to do in such large volumes that i would need a specific built in function / toolbox / plugin to do for me.
Thank you all.

Accepted Answer

Sean de Wolski
Sean de Wolski on 20 Jul 2011
m = n;
m(~mod(m,2)&m~=2) = []
remove parts that are divisible by two (~mod(m,2)) and not two, m~=2
  9 Comments
Oleg Komarov
Oleg Komarov on 20 Jul 2011
BTW my troll answer has an error, it's | and not &
Chase McDermott
Chase McDermott on 3 Nov 2015
I am also new to Matlab, why do you have to put the "~" before the mod function?

Sign in to comment.

More Answers (1)

Jan
Jan on 20 Jul 2011
The best method to learn Matlab is to use it. So how would you solve this?
One idea: Start from "n = 1:1:20" and look what happens, if the step width is set to 2: "1:2:20".
  3 Comments
Sean de Wolski
Sean de Wolski on 20 Jul 2011
'lookfor'
will be your friend along with help.
Jan
Jan on 20 Jul 2011
And of course the "Getting started" chapters of the documentation.

Sign in to comment.

Categories

Find more on Historical Contests in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!