# extract first row from matrix

2,692 views (last 30 days)

Show older comments

I'm trying to extract the first row from my matrix and put in a vector like [m,n]

my matrix is 2x5 size.

but I'm getting errors, Indexing cannot yield multiple results.

Please any solution to that?

##### 1 Comment

Maryam
on 13 Feb 2024

Moved: Dyuman Joshi
on 2 Apr 2024 at 9:52

how we display only 2nd and 5th row of matrix T in matrix X in matlab

### Answers (8)

Hikaru
on 5 Feb 2015

You have the wrong syntax.

Lets say A is your 2x5 matrix.

A = rand(2,5)

Then to extract the first row, you simply use:

V = A(1,:);

##### 4 Comments

Stephen23
on 5 Feb 2015

Edited: Stephen23
on 7 Feb 2015

MathWorks Support Team
on 27 Nov 2018

To extract any row from a matrix, use the colon operator in the second index position of your matrix. For example, consider the following:

A = [1 2 3; 4 5 6];

row1 = A(1,:)

row2 = A(2,:)

“row1” is the first row of “A”, and “row2” is the second row.

For more on basic indexing, see:

##### 1 Comment

Stephen Porter
on 28 Nov 2020

Stephen23
on 5 Feb 2015

Edited: Stephen23
on 5 Feb 2015

What you are trying to do is create a comma-separated list from a numeric vector. As far as I know, there is no inbuilt way to achieve this. This question has also been asked before , you might find other discussions on this online.

Usually in MATLAB it makes a lot more sense to keep your data together in arrays, so it may not be necessary to do this multiple-assignment. You should consider this. However if it really is necessary to do this multiple assignment, then you could:

- Assign individually: n = A(1,1); m = A(1,2);
- Assign via a cell array:

B = num2cell(A(1,1:2));

[n,m] = B{:};

Although the first option is much clearer!

EDIT: on re-reading your question, it seems that perhaps all you are after is just the vector of the first row of A, in which case you do not need to assign to two separate values first, as you can just extract the vector directly:

V = A(1,:);

If you just need the row (vector), then there is no need in MATLAB to create intermediate variables m,n.

##### 1 Comment

Lizan Mohammed
on 1 Mar 2021

naazneen vhora
on 11 May 2018

if i wanted to extract a certain column without the first element how would i do that.

for example, if my column has the values: 2,3,4,5,6... and i want to access all the values in the column after 2 how would i do that? Yes i mean column for typing reasons i wrote it horizontally.

##### 1 Comment

Stephen23
on 11 May 2018

Using basic MATLAB indexing:

M(2:end,C)

for column C of matrix M. Basic MATLAB indexing is explained in the introductory tutorials:

KAMOOSH BABA SHAIK
on 1 Apr 2021

You can combine indexing with assignment to change array values to equal other elements. For example, this code would change the value of x(1) to x(2):

x(1) = x(2)

Try changing the first column of data to the second column of data.

ans : data(:,1) = data(:,2)

##### 0 Comments

SANTOSH MOHAPATRA
on 17 Apr 2019

I have a matrix of dimension(256, 32), means rows=256 and column=32. I want to select 128 rows of the particular matrix and make another Vector of dimension (1, 256*32) means to represent all the elements in a single row. How can I do that?

Please help...can I apply a loop program for it??

##### 1 Comment

Stephen23
on 17 Apr 2019

Abhishek Mishra
on 21 Oct 2020

You can combine indexing with assignment to change array values to equal other elements. For example, this code would change the value of x(1) to x(2):

x(1) = x(2)

Try changing the first column of data to the second column of data.

Answer for this please

##### 0 Comments

Guna
on 2 Apr 2024 at 9:13

% The matrix X has been created for you.

X = [1:100;101:200;201:300;301:400;401:500;501:600];

% Extract the second row

y =

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!