How would I check to see if the number of rows in one matrix are equal to the number of rows in another matrix?

조회 수: 4 (최근 30일)
I'm trying to write a function which will compare two matrices. Prior to this comparison I want my function to error check the entered matrices to ensure that the two matrices have the same number of rows. I know I will use and if/else statement and an error message, but what would the code look like for the check? Thank you!

답변 (2개)

Star Strider
Star Strider 2016년 11월 2일
Use the size function to get the size of the vector or matrix. The first dimension are the number of rows, so for matrix ‘A’:
nr_rows = size(A,1);

Thorsten
Thorsten 2016년 11월 3일
편집: Thorsten 2016년 11월 3일
You can use assert
assert(size(A,1) == size(B, 1), 'Matrices have different numbers of rows.')
If the condition given as the first argument is false, an error message with the second argument is thrown.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by