Removing Multiples of a number from a Matrix

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

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

Just tested this and it works!! awesome, thank you!
Sorry for being a noob but would you mind explaining what you just did here? =)
m = m(logical(mod(m,2)) & m == 2); % (troll)
exactly what I said:
I created a logical index of all values divisible by two:
~mod(m,2)
I then found all value of m not equal to two:
m~=2
I then used an AND to require that it be divisible be two AND not equal to two.
The value that were both I deleted by setting them to the empty set
= []
Oleg, you and your glass half full optimism!! Wednesdays are clearly meant to be glass half empty...
Ah thank you, I just ran "help" right before i read this. Thanks again, when i meant new to matlab i also meant coding / programming in general, i have 0 prior knowledge.
Thank you both for posting.
Welcome to MATLAB, programming and MATLAB answers!
A good idea to learn is to break every statement you see into its smallest components (as I did above), figure out what each one does and how they add up.
Thank you again, by the way would you have any recommendation in terms of books for complete beginners? I have never had a course on programming or anything. Currently I am using Essentials of Matlab by Hahn but a rather old edition i found in the lab. I use R2011a but i think this book is for an older version.
BTW my troll answer has an error, it's | and not &
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

Thank you for your comment. While I know how to solve problems I see in a book on paper, its a different story on matlab. So therefore I create problems myself and see How i could solve them using matlab.
I'm also completely new to programming and Matlab is the first programming software/language I have ever used (just started a week ago). I also dont have a good book so I am trying to google and use the help functions as I try my best to learn Matlab.
'lookfor'
will be your friend along with help.
And of course the "Getting started" chapters of the documentation.

Sign in to comment.

Categories

Find more on Get Started with MATLAB 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!