-1

I am trying out the questions in programming assignment of Coursera course on Matlab programming as an exercise. This is for my self-study.

Question:

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.

My code snippet:

function num = classify(x)
num = -1;
if(x == 3.14159265358979)
    num = 0;
elseif(size(x, 2) == 3)
    num = -1;
end
end

I got the below result on Matlab.

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 performed correctly for argument(s) 3.14159265358979
    Feedback: Your function made an error for argument(s) [1 2 3]

Am I doing something wrong for arguments [1 2 3]?

tmgr
  • 1,187
  • 3
  • 14
  • 16
  • 1
    how does the code snippet fit to the question? – Steffen May 21 '15 at 10:24
  • 1
    Agreed, the code does not do any of things mentioned in the question – am304 May 21 '15 at 10:25
  • 1
    You should really just try running your program with the problematic input, and see what the debugger tells you! – Jens Boldsen May 21 '15 at 10:27
  • Oh, and I agree with the above commenters, it's not that you are doing anything wrong for a specific case, it's that your code has no relation to the problem stated and only works in very specific cases some of which happens to be some of the ones tested for. – Jens Boldsen May 21 '15 at 10:31
  • Your code at no point outputs either `1` or `2` which are requirements so how can it work? Also this function will come in handy: http://www.mathworks.com/help/matlab/ref/ndims.html – Dan May 21 '15 at 10:33

6 Answers6

2

If all you want is the code, I'll provide you with the answer, but I suggest you sit down and try to understand how and why this codes solves the problem, I'm guessing you want to learn something from it. Well, here's the code:

function [num] = classify(x)
   if numel(x) == 0
      num = -1;
      return
   end
   num = sum(size(x) > 1);
end
Jens Boldsen
  • 1,255
  • 13
  • 21
  • This is the best answer, but it needs a check for dimensions higher than `2` as these should still just return `2`. – Dan May 21 '15 at 10:45
  • 1
    Quoting from your problem statement: "That argument will have no more than two dimensions.", I'm guessing that means I don't have to check whether there are more than two dimensions. – Jens Boldsen May 21 '15 at 10:46
1

You can most easily check if x is empty or a scalar by counting the number of elements (i.e. use the numel function). Then to determine if it is a vector or a higher dimensional matrix you need to check if the number of dimensions is less than 3 (this is because ndims returns 2 for both 1D and 2D matrices) and also verify that at least one of the first two dimensions has a size of 1:

function num = classify(x)
    n = numel(x);
    if n < 2
        num = n-1;
    else
        if ndims(x) < 3 && any(size(x) == 1)
            num = 1;
        else
            num = 2;
        end
    end
end
Dan
  • 45,079
  • 17
  • 88
  • 157
  • Got error for your solution .. `Your function made an error for argument(s) [0 0 1 0 1 1 1 0;1 1 0 0 0 0 1 1;1 1 0 1 0 1 1 1;0 1 0 1 0 1 1 0;1 1 1 1 0 ` – tmgr May 21 '15 at 10:39
  • @tmgr do you understand the solution or why your was wrong? If not, then you've wasted your time. Make sure you really understand this. Try make the same function but slightly differently – Dan May 21 '15 at 10:46
0

You should aim to write a generic function, your function would not work for example for any scalar input than pi.

Use the size function to determine the dimensions of the input, for example:

>> size([])
ans = 0     0

>> size(5)
ans = 1     1 

>> size([5 6 7])
ans =  1     3

>> size([5;6;7])
ans =  3     1

Based on this, your function could looke like this:

function [num] = classify(x)

s = size(x);
if s == [0 0]
    num = -1;
elseif s == [1 1]
    num = 0;
elseif sort(s) == [1 3]
    num = 1;
else
    num = 2;
end

end
m.s.
  • 16,063
  • 7
  • 53
  • 88
  • What about returning `2`? – Dan May 21 '15 at 10:33
  • @m.s.The solution is not working. Gave this error. `Problem 4 (classify): Feedback: Your function performed correctly for argument(s) [] Feedback: Your function made an error for argument(s) zeros(1,0) Your solution is _not_ correct.` – tmgr May 21 '15 at 10:38
0

Yes, you tring to compare an array with a float. It allows to do that (programaticalyl wrong) thing for [] because the array is empty And for zeros, because the array is empty again: in the first case 0 columns, and in the second 0 rows

elMestre
  • 394
  • 4
  • 7
0
function i=classify(x)
[m, n]=size(x);
if n==1 && m==1
    i=0;
elseif (m==0 && n==0)|| (m>=1 && n==0) || (m==0 && n>=1)
    i=-1;
elseif (n>=1 && m==1) || (n==1 && m>=1)
    i=1;
else i=2;
end
0
function y=classify(x) 
    [a b]=size(x);

    %check for empty matrix
    % Do not forget that an empty matrix can be size a x 0 or 0x a, where a can be 
    % arbitrary number

    if (a>0)&&(b==0)||(a==0)&&(b>0)||(a==0)&&(b==0)
        y=(-1);

    %check for scalar
    elseif (a==1)&&(b==1)
        y=0;

    %check for vector 
    elseif (a>=1)&&(b==1)||(a==1)&&(b>=1)
        y=1;

    %other case
    else
        y=2;
end
Brian Ray
  • 1,482
  • 3
  • 20
  • 20
Kenz
  • 1
  • 1