Removing Multiples of a number from a Matrix
4 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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
Chase McDermott
on 3 Nov 2015
I am also new to Matlab, why do you have to put the "~" before the mod function?
More Answers (1)
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
See Also
Categories
Find more on Historical Contests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!