Number of Rows, Number of Columns…

Just a shorty entry: I frequently need to determine the number of rows or columns in a matrix - you can call the size() function for this, but calling size with two arguments all the time makes my code look cluttered (IMHO). So I wrote a couple of tiny functions which return the number of rows or columns, respectively.

function [nr] = nrows(A)
% NROWS Number of rows in an array.
%
%   [nr] = NROWS(A) returns number of rows in A.
	
nr = size(A, 1);
	
function [nc] = ncols(A)
% NCOLS Number of columns in an array.
%
%   [nr] = NCOLS(A) returns number of columns in A.
	
nc = size(A, 2);
Back to top
  1. Onur

    Monday, August 10, 2009 - 18:52:50

    Great article. Thanks