Easy question from a newbie

4 ビュー (過去 30 日間)
Andrew
Andrew 2013 年 3 月 8 日
This should be a really easy question for some of you...I think.
Is there a function to specifically call out the number of rows in matrix? Length usually works but it takes the "longest side" so if you have more rows than columns, it counts the columns. I know size returns both the # of rows and columns, but I'm tweaking somebody else's more complicated code, so I just need a function to replace length where its reading # of columns instead of # of rows.
Thanks

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 8 日
編集済み: Azzi Abdelmalek 2013 年 3 月 8 日
size(A,1) % number of rows
size(A,2) % number of columns
size(A,3) % number of channels

その他の回答 (3 件)

Image Analyst
Image Analyst 2013 年 3 月 8 日
編集済み: Image Analyst 2013 年 3 月 8 日
Use the size() function:
theSize = size(yourArray);
theSize will be a list of all the dimension sizes in your array, however many there might be, for example [2,3,2,5,16] for a 5 dimensional array. If you just want the rows, which is the first dimension, you can do this:
numberOfRows = size(yourArray, 1);
If you wanted the number of columns, you'd specify 2 instead of 1, and so on.
Or if you know it's two dimensions:
[rows, columns] = size(yourArray);
Or if you know it's 3 dimensions, like a color image:
[rows, columns, numberOfColorChannels] = size(yourImageArray);

Ruben
Ruben 2013 年 3 月 8 日
you can use the size command for this. Just extract the first value of the output of the size command. For example:
A = rand(2,3)
temp = size(A)
rows = temp(1)
will return 2, this is the number of rows.

Ryan Livingston
Ryan Livingston 2013 年 3 月 8 日
I would just write a little function to do it:
function n = ncols(A)
s = size(A);
n = s(1);

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by