how to make a function that discreminate between scalar and vectors in matlab

Hi; I am going to make a function that classify inputs.If the input is a vector it generate 0 and if it is empty scalar matrix it gives -1 and for matrix it gives 1 I am using that codes
function myclass=classify(x)
if size(x)==0 0
myclass=-1;
elseif size(x)~=0 0
myclass=1;
end
end
This codes run perfectly for empty matrix and matrix but i getting in troble how i can define it to discriminate for vector(having 1 row or col). I hope i have to use two more ifs in elseif one will check for vector and other for matrix. I stuck how it implement .Kindly guide me about correction..Thanks in advacne

2 commentaires

This codes run perfectly for empty matrix and matrix but i getting in troble how i can define it to discriminate for vector(having 1 row or col). I hope i have to use two more ifs in elseif one will check for vector and other for matrix. I stuck how it implement .Kindly guide me about correction..Thanks in advacne
Stephen23
Stephen23 le 20 Mai 2015
Modifié(e) : Stephen23 le 20 Mai 2015
What exactly is an "empty scalar matrix"? By definition a scalar is a 1x1 array, and thus cannot be empty. This is clearly documented here:
In any case, what is wrong with the inbuilt functions: isempty, isscalar, isvector, ismatrix, isrow, iscolumn... ? Did they stop working?
Maybe reading this would help:

Connectez-vous pour commenter.

 Réponse acceptée

You need to use the any and all functions in your comparisons:
function myclass=classify(x)
myclass = 0;
if any(size(x)==0)
myclass=-1;
elseif all(size(x)>1)
myclass=1;
end
end
This worked for me.

10 commentaires

@Star Strider thanks for contributions.I do not want to use build in functions that why i am using if statements. your codes looks it will run for matrix(non empty or empty). What about vectors.I want to make function which besides matrix(empty or non empty) check also for vectors.in the case of vector it gives 0 in output.guide me about vector
Please define what you mean by ‘built-in functions’. What functions can you use? Are you allowed max and min and some others?
The problem is that all functions are ‘built-in’, including addition, multiplication, comparison, branching, and everything else.
function myclass=classify(x)
myclass = 1;
if any(size(x)==0)
myclass=-1;
elseif all(size(x)>1)
myclass=0;
end
end
these corrections i have to made.It was really interesting question.vote up it please. Thanks for assistance
Write a function called classify that takes one input argument x. That argument will have no more than two dimensions. If x is an empty matrix, the function returns -1. If x is a scalar, it returns 0. If x is a vector, it returns 1. Finally, if x is none of these, it returns 2. Do not use the built-in functions isempty, isscalar, or isvector. This was my problem.For the better understand i share whole question.Tell me what is meant by the line "That argument will have no more than two dimensions". I understand it means classify takes input as vector or scalar.but when i run with that assumption i got an error.Tell me what is meany by that line.
My pleasure.
That the ‘argument will have no more than two dimensions’ means that it has to be empty (0x0), a scalar (1x1), a vector (1xN) or (Nx1), or a 2-D matrix (MxN). It eliminates the problem of having to consider multi-dimensional matrices (LxMxN).
Thanks @Star.. To make my function according to question mentioned in the above comment. i am using this code
function myclass=classify(x)
myclass = 1;
if any(size(x)==0)
myclass=-1;
elseif all(size(x)>1)
myclass=0;
end
end
when i run this in the answer checker provided me i got an error like this
Problem 4 (classify):
Feedback: Your function performed correctly for argument(s) []
Feedback: Your function performed correctly for argument(s) zeros(1,0)
Feedback: Your function performed correctly for argument(s) zeros(0,4)
Feedback: Your function made an error for argument(s) 3.14159265358979
Your solution is _not_ correct.
tell me where is the problem with my code.Kindly read the question mentioned above to better understand the problem..Thanks in advance
If it’s a scalar, all dimensions have to be equal to 1, so this statement has to test for that:
elseif all(size(x)>1)
See if substituting the ‘double equal sign’ == helps.
Also, your default condition needs to be set to be sure it returns the correct value as described in: ‘Finally, if x is none of these, it returns 2.’

Connectez-vous pour commenter.

Plus de réponses (2)

Thorsten
Thorsten le 20 Mai 2015
Modifié(e) : Thorsten le 20 Mai 2015
You should use Matlab's functions for your task: isempty, isscalar, ismatrix. Further, classify is a function in the statistics toolbox; you may want to choose a different name.
Stephen23
Stephen23 le 20 Mai 2015
Modifié(e) : Stephen23 le 20 Mai 2015
This would be much simpler code without the if commands, even as an anonymous function:
>> fun = @(x) all(size(x)>1)-isempty(x);
>> fun([])
ans =
-1
>> fun([1,2,3])
ans =
0
>> fun([1,2,3;4,5,6])
ans =
1
To do the whole thing without "inbuilt functions" then just replace the isempty(x) with any(size(x)==0). I doubt that any solution fulfills this though:

Catégories

En savoir plus sur Get Started with MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by