Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Iterator
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== MATLAB === [[MATLAB]] supports both external and internal implicit iteration using either "native" arrays or <code>cell</code> arrays. In the case of external iteration where the onus is on the user to advance the traversal and request next elements, one can define a set of elements within an array storage structure and traverse the elements using the <code>for</code>-loop construct. For example, <syntaxhighlight lang="matlab"> % Define an array of integers myArray = [1,3,5,7,11,13]; for n = myArray % ... do something with n disp(n) % Echo integer to Command Window end </syntaxhighlight> traverses an array of integers using the <code>for</code> keyword. In the case of internal iteration where the user can supply an operation to the iterator to perform over every element of a collection, many built-in operators and MATLAB functions are overloaded to execute over every element of an array and return a corresponding output array implicitly. Furthermore, the <code>arrayfun</code> and <code>cellfun</code> functions can be leveraged for performing custom or user defined operations over "native" arrays and <code>cell</code> arrays respectively. For example, <syntaxhighlight lang="matlab"> function simpleFun % Define an array of integers myArray = [1,3,5,7,11,13]; % Perform a custom operation over each element myNewArray = arrayfun(@(a)myCustomFun(a),myArray); % Echo resulting array to Command Window myNewArray function outScalar = myCustomFun(inScalar) % Simply multiply by 2 outScalar = 2*inScalar; </syntaxhighlight> defines a primary function <code>simpleFun</code> that implicitly applies custom subfunction <code>myCustomFun</code> to each element of an array using built-in function <code>arrayfun</code>. Alternatively, it may be desirable to abstract the mechanisms of the array storage container from the user by defining a custom object-oriented MATLAB implementation of the Iterator Pattern. Such an implementation supporting external iteration is demonstrated in MATLAB Central File Exchange item [http://www.mathworks.com.au/matlabcentral/fileexchange/25225 Design Pattern: Iterator (Behavioral)]. This is written in the new class-definition syntax introduced with MATLAB software version 7.6 (R2008a) and features a one-dimensional <code>cell</code> array realization of the [[List (computing)|List Abstract Data Type]] (ADT) as the mechanism for storing a heterogeneous (in data type) set of elements. It provides the functionality for explicit forward [[List (computing)|List]] traversal with the <code>hasNext()</code>, <code>next()</code> and <code>reset()</code> methods for use in a <code>while</code>-loop.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Iterator
(section)
Add topic