How can I check this matrix is the correct size?

조회 수: 46 (최근 30일)
Heather Spain
Heather Spain 2020년 4월 25일
답변: kevin harianto 2022년 4월 4일
I am wondering if this code is the correct code to check if the matrix is 18x4.
I think all it does is check if it has 18 rows, but does it check if it has 4 columns?
(initial4 should be an 18x4 matrix)
sz= size(initial4);
if (sz ~= 18)
fprintf("Vector must be an 18x4 matrix.");
end

답변 (2개)

Ameer Hamza
Ameer Hamza 2020년 4월 25일
Use this for checking the size
if ~isequal(size(M1), [18 4])
For example
M1 = rand(18, 4);
M2 = rand(18, 3);
if ~isequal(size(M1), [18 4])
fprintf("Matrix M1 must be an 18x4 matrix.\n");
end
if ~isequal(size(M2), [18 4])
fprintf("Matrix M2 must be an 18x4 matrix.\n");
end
Result
Matrix M2 must be an 18x4 matrix.
  댓글 수: 2
Heather Spain
Heather Spain 2020년 4월 27일
Thank you, that worked!
Ameer Hamza
Ameer Hamza 2020년 4월 27일
Glad to be of help.

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


kevin harianto
kevin harianto 2022년 4월 4일
what if you want to check if the size of the matrix is too small?

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by