count rows in matrix ?

조회 수: 1,875 (최근 30일)
Rakesh Praveen
Rakesh Praveen 2011년 11월 7일
편집: MathWorks Support Team 2020년 11월 4일
hey, i want to know is there a way to get how many rows are there in a given matrix. like for example when we say 'length(A)' it can give number of columns in a matrix(A), likewise can we know about number of rows ?
thank you.
  댓글 수: 3
Image Analyst
Image Analyst 2016년 10월 16일
height() doesn't work on matrices -- it only works on tables.
morteza HEIDARI
morteza HEIDARI 2017년 8월 15일
편집: morteza HEIDARI 2017년 8월 15일
If for example your matrix is A, you can use : size(A,1) for number of rows. size(A,2) for number of columns. Also there are some other ways like : length ( A(:,1) ) for number of rows.

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

채택된 답변

Oleg Komarov
Oleg Komarov 2011년 11월 7일
편집: MathWorks Support Team 2020년 11월 4일
size(A,1)
The following documentation provides more information on 'size' function:
Starting in R2020b, you can also use "height" and "width" function to count rows and columns of a matrix respectively.
For example:
A=rand(3,2); %Defining a matrix;
count_rows=height(A); %Computes number of rows in A
count_columns=width(A); %Computes number of columns in A
For more information on "height" and "width" functions, please refer to the following documentation:
  댓글 수: 3
Hoang-Linh To
Hoang-Linh To 2014년 5월 19일
Thank you so much, that's also what I want :)
Pooja
Pooja 2014년 9월 22일
It workd for me also. Thankyou Oleg Komarov

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

추가 답변 (5개)

Ashutosh Kumar
Ashutosh Kumar 2016년 7월 22일
length(A) gives you maximum out of the matrix made by calling the size,so it doesn't give you column(A) and for calling column(A) you need size(A,2) and for row you need size(A,1)...like suppose you have a 5*4 matrix then length(A) will give you 5 number of rows not 4...Hope that will help others I myself used length(A) and ended up making a wrong code and took me 2 hours to do it right

Image Analyst
Image Analyst 2011년 11월 7일
For a 2D matrix:
[rows columns] = size(array2D);
or, in general,
sizeOfMatrix = size(yourMatrix);
  댓글 수: 1
Rakesh Praveen
Rakesh Praveen 2011년 11월 7일
thank u very much image analyst

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


Thulitha Theekshana
Thulitha Theekshana 2019년 7월 17일
I think the best way is to use size(x,y) function.
ex: if you want to get the height, size(matrix_name, 1). Here the second variable denotes the dimensioin you need to measure. From one it means the number of rows or the first dimension. From 2 it means the number of columns or the length. So I think you get the idea,

Gaganjyoti Baishya
Gaganjyoti Baishya 2020년 6월 20일
It's simple. You can see the size of the first column, that will be the rows in matrix.
rows = size(myMatrix, 1); % first row length

Anurag Pratap Singh
Anurag Pratap Singh 2020년 6월 25일
Hi Rakesh
For counting the number of rows in a matrix you can use the size funtction and pass your matrix in it
[NumRows NumCols]=size(your_matrix);
The first output is the Number of rows .
Thank you

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by