Skip to Main Content
Programming in MATLAB ®: A Problem-Solving Approach by Pearson
book

Programming in MATLAB ®: A Problem-Solving Approach by Pearson

by Ram N. Patel, Ankush Mittal
May 2024
Intermediate to advanced content levelIntermediate to advanced
433 pages
13h 7m
English
Pearson India
Content preview from Programming in MATLAB ®: A Problem-Solving Approach by Pearson
278 Ñ Efficient Coding Using Vectorization Technique
Let us now consider the vectorized implementation of the code. The “for” loop used to
determine if the counter value is divisible (i.e., loop corresponding to for i = 1:no_
found) can be vectorized as shown in the code here:
kprime_mod3.m
function val_arr = kprime_mod3(k)
% Vectorized version
val_arr = 2*ones(1,k);
% Pre-assign val_arr to be a array of ’’s
% Since counter would be divided by val_arr
% elements, all the elements are initialized to
% 2 and not 1
counter = 3;
no_found = 1;
while no_found < k % Loop till k primes are found
if all(rem(counter,val_arr(1:no_found)))
no_found = no_found ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Applied Numerical Methods Using MATLAB, 2nd Edition

Applied Numerical Methods Using MATLAB, 2nd Edition

Won Y. Yang, Wenwu Cao, Jaekwon Kim, Kyung W. Park, Ho-Hyun Park, Jingon Joung, Jong-Suk Ro, Han L. Lee, Cheol-Ho Hong, Taeho Im
MATLAB Recipes: A Problem-Solution Approach

MATLAB Recipes: A Problem-Solution Approach

Michael Paluszek, Stephanie Thomas
Practical MATLAB Deep Learning: A Projects-Based Approach

Practical MATLAB Deep Learning: A Projects-Based Approach

Michael Paluszek, Stephanie Thomas, Eric Ham

Publisher Resources

ISBN: 9781306983440Publisher Website