Odd Numbers with missing multiples of a number
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi guys, trying to figure out how to create a function that will allow me get all odd numbers but skip multiples of 3? Also would like to achieve this without adding a for loop. These are the tests:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238291/image.png)
Any help to set me on track would be appreciated
This is the code I have so far:
function out = oddsNoFives(a)
x = 1:2:a;
disp(x)
if mod(x,10)~=3
disp(x)
end
end
Im just trying to create the basic array from 1 to a to start with then work my way up from there. I dont understand why my "if mod(x,10)~=3" is not working properly either. Not very fluent so just trying to get tips but i understand the math involved
0 Kommentare
Antworten (1)
madhan ravi
am 14 Sep. 2019
I leave the understanding part to the reader to understand whats going on:
oddsNoFives=@(x,y)nonzeros((~sum(~mod(1:x,[2;y]),1)).*(1:x)).'
% Calling the function
oddsNoFives(30,[])
oddsNoFives(30,5)
oddsNoFives(30,7)
1 Kommentar
madhan ravi
am 14 Sep. 2019
Bearbeitet: madhan ravi
am 14 Sep. 2019
Prerequisites:
https://blogs.mathworks.com/loren/2013/02/20/logical-indexing-multiple-conditions/ - one of my favorite blogs
https://in.mathworks.com/help/matlab/matlab_prog/matlab-operators-and-special-characters.html - Tilde (not())
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!