I'm trying to create a function that will only accept a matrix

3 vues (au cours des 30 derniers jours)
Hector Koch
Hector Koch le 29 Mar 2016
Modifié(e) : Stephen23 le 29 Mar 2016
I'm not really sure how to go about this challenge. I know something is wrong with my output and function but I do not really know how to work it etc.
start
function output=rowcolumn(x,y)
if length(x)==length(y) disp('yes')
if length(x)>length(y) disp('no')
if length(x)<length(y) disp('no')
end
  2 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 29 Mar 2016
What is your problem?
Stephen23
Stephen23 le 29 Mar 2016
Modifié(e) : Stephen23 le 29 Mar 2016
Whatever you do avoid using length, as its output chagnes depending on the matrix orientation. Far more robust are numel and size for comparing the dimensions and number of elements.
Avoiding using length will be the first step to more robust code :)

Connectez-vous pour commenter.

Réponses (1)

Star Strider
Star Strider le 29 Mar 2016
Modifié(e) : Star Strider le 29 Mar 2016
I guess you’re testing for a vector lengths.
You need to use elseif to test the alternatives:
function output=rowcolumn(x,y)
if length(x)==length(y) disp('yes')
elseif length(x)>length(y) disp('no')
elseif length(x)<length(y) disp('no')
end
end

Catégories

En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by