This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Insert an element into a list at a given position

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


listlib::insertAt(list, element, <pos>)


listlib::insertAt(list, element, pos) inserts element into list at position pos.

With the function listlib::insertAt any element can be inserted into any list at a specified place.

The third argument (the "insert index") determines the place to insert the element into the given list.

If the insert index is less than 1 the element is inserted in front of the list. If the insertion index is greater than nops(list) the element is appended to the list. To append an element to a list the kernel function append is faster.

If no third argument is given, the given element is inserted in front of the list.

If the argument element is a list too, the elements of this list will be inserted (or appended) instead of the whole list by preserving the order.


Example 1

Insertion 2 at the third place of the given list:

listlib::insertAt([1, 1, 1], 2, 3)

Insertion of an element in front of a list. The third argument is optional in this case:

listlib::insertAt([1, 1, 3, 1], 2, 0), listlib::insertAt([1, 1, 3, 1], 2)

Appending of an element. This could be done faster with append:

listlib::insertAt([1, 2, 3], 4, 1000), append([1, 2, 3], 4)



A list


Any MuPAD® object


Any integer

Return Values

Given list enlarged with the inserted element

See Also

MuPAD Functions

Was this topic helpful?