# extract first row from matrix

조회 수: 9,202(최근 30일)
Hello Matlab 2015년 2월 5일
답변: KAMOOSH BABA SHAIK 2021년 4월 1일
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.
##### 댓글 수: 1표시숨기기 없음
Zabir Al Nazi Nabil 2017년 4월 4일
To extract first row from matrix X, you can do this - XX = X(1:1,:); Then, to copy it in a vector, you just do this - Y = XX;

댓글을 달려면 로그인하십시오.

### 답변(7개)

Hikaru 2015년 2월 5일
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,:);
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Stephen 2015년 2월 5일
@Hello Matlab: Your explanation is inconsistent: "...to get the first row to a vector of 1x2..." and "assign those two value to [m,n]" means two quite different things. Do you want to obtain the vector of the first row, OR to get two variables m,n corresponding to the elements of the first row of the matrix? So far Hikaru and I have addressed both of these, so if there is something that is not clear, you need to explain it to us again.

댓글을 달려면 로그인하십시오.

MathWorks Support Team 2018년 11월 27일
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표시숨기기 없음
Stephen Porter 2020년 11월 28일
If you use a for loop to iterate so many times, how do you acess the first elemeent after the loop is complete?

댓글을 달려면 로그인하십시오.

Stephen 2015년 2월 5일
편집: Stephen 2015년 2월 5일
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:
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표시숨기기 없음
Lizan Mohammed 2021년 3월 1일
What is the command if I want to have a matrix that has a column of all elements as 273, %our number of elememts is 38

댓글을 달려면 로그인하십시오.

naazneen vhora 2018년 5월 11일
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표시숨기기 없음
Stephen 2018년 5월 11일
Using basic MATLAB indexing:
M(2:end,C)
for column C of matrix M. Basic MATLAB indexing is explained in the introductory tutorials:

댓글을 달려면 로그인하십시오.

SANTOSH MOHAPATRA 2019년 4월 17일
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?
##### 댓글 수: 1표시숨기기 없음
Stephen 2019년 4월 17일
No loop required, just use indexing and reshape (and possibly transpose, depending on the order you want).

댓글을 달려면 로그인하십시오.

Abhishek Mishra 2020년 10월 21일
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.
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

KAMOOSH BABA SHAIK 2021년 4월 1일
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)

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!