head
Get top rows of array or table
Description
head(
displays the first eight
rows of array, table, or timetable A
)A
in the Command Window
without storing a value.
returns
the requested rows of B
= head(___)A
for either of the previous syntaxes.
The data type of B
is the same as the data type of
A
.
Examples
Create a matrix with 100 rows, and display the first eight rows of the matrix.
If you do not specify an output argument, head
does not return a value. It only displays the top of the matrix.
A = repmat((1:100)',1,4); head(A)
1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8
Create a table from a file with 1468 rows.
T = readtable("outages.csv","TextType","string"); size(T)
ans = 1×2
1468 6
Display the first three rows. If you do not specify an output argument, head
does not return a value. It only displays the top of the table.
head(T,3)
Region OutageTime Loss Customers RestorationTime Cause ___________ ________________ ______ __________ ________________ ______________ "SouthWest" 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 "winter storm" "SouthEast" 2003-01-23 00:49 530.14 2.1204e+05 NaT "winter storm" "SouthEast" 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 "winter storm"
Create a table by reading data from a spreadsheet. Display the size of the table, showing that it has 1468 rows.
T = readtable("outages.csv","TextType","string"); size(T)
ans = 1×2
1468 6
Return another table that has the first eight rows of T
.
T2 = head(T)
T2=8×6 table
Region OutageTime Loss Customers RestorationTime Cause
___________ ________________ ______ __________ ________________ _________________
"SouthWest" 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 "winter storm"
"SouthEast" 2003-01-23 00:49 530.14 2.1204e+05 NaT "winter storm"
"SouthEast" 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 "winter storm"
"West" 2004-04-06 05:44 434.81 3.4037e+05 2004-04-06 06:10 "equipment fault"
"MidWest" 2002-03-16 06:18 186.44 2.1275e+05 2002-03-18 23:23 "severe storm"
"West" 2003-06-18 02:49 0 0 2003-06-18 10:54 "attack"
"West" 2004-06-20 14:39 231.29 NaN 2004-06-20 19:16 "equipment fault"
"West" 2002-06-06 19:28 311.86 NaN 2002-06-07 00:51 "equipment fault"
Input Arguments
Input data, specified as an array, cell array, table, or timetable.
Number of rows to extract, specified as a positive integer. If
A
has fewer than k
rows, then
head
returns all rows of
A
.
Extended Capabilities
The
head
function fully supports tall arrays. For more information,
see Tall Arrays.
You can use head
and tail
with tall
arrays of any valid underlying data type (single
,
double
, int8
, datetime
,
table
, and so on).
If you are unsure whether the result returned by gather(A)
will fit in memory, then use gather(head(A))
or
gather(tail(A))
. These commands still fully evaluate the tall
array A
but return only a small subset of the result in
memory.
The head
function
fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray
(Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2016bDisplay or return the top rows of a vector, matrix, multidimensional array, or
cell array using head
. In previous releases,
head
supports only tables, timetables, and tall
arrays.
When you call head
without a specified output argument,
head
displays the selected rows of the table but does not
store the output in the ans
variable. In previous releases,
calling head
without a specified output argument causes the
output to be stored in ans
.
Calling head
in a live script is usually not recommended.
Instead, display the table or timetable by typing the variable name with no semicolon. The
Live Editor provides a widget that enables you to examine the entire table or timetable.
However, if you do call head
in a live script, it is recommended
that you assign the output to a variable so that the live script creates a widget for the
output.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)