Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Array slicing
(section)
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!
===1998: [[S-Lang]]=== Array slicing was introduced in version 1.0. Earlier versions did not support this feature. Suppose that A is a 1-d array such as <pre> A = [1:50]; % A = [1, 2, 3, ...49, 50] </pre> Then an array B of first 5 elements of A may be created using <pre> B = A[[:4]]; </pre> Similarly, B may be assigned to an array of the last 5 elements of A via: <pre> B = A[[-5:]]; </pre> Other examples of 1-d slicing include: <pre> A[-1] % The last element of A A[*] % All elements of A A[[::2]] % All even elements of A A[[1::2]] % All odd elements of A A[[-1::-2]] % All even elements in the reversed order A[[[0:3], [10:14]]] % Elements 0-3 and 10-14 </pre> Slicing of higher-dimensional arrays works similarly: <pre> A[-1, *] % The last row of A A[[1:5], [2:7]] % 2d array using rows 1-5 and columns 2-7 A[[5:1:-1], [2:7]] % Same as above except the rows are reversed </pre> Array indices can also be arrays of integers. For example, suppose that <code>I = [0:9]</code> is an array of 10 integers. Then <code>A[I]</code> is equivalent to an array of the first 10 elements of <code>A</code>. A practical example of this is a sorting operation such as: <pre> I = array_sort(A); % Obtain a list of sort indices B = A[I]; % B is the sorted version of A C = A[array_sort(A)]; % Same as above but more concise. </pre>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)