How can I check this matrix is the correct size?
40 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!