Question

I am a little bit rusty on Oop. I have following code.

classdef diag2by2
    properties
        a;
        b;
    end
    methods
        function obj = diag2by2(a, b)
            obj.a = a;
            obj.b = b;
        end
        function obj = plus(obj1, obj2)
            temp = [obj1.a,  0; 0 obj1.b]+ [obj2.a, 0; 0 obj2.b];
            obj.a = temp(1,1);
            obj.b = temp(2,2);
        end
        function obj = minus(obj1, obj2)
            temp = -1*obj2;
            obj = plus(obj1, temp);
        end
        function obj = mtimes(obj1, obj2)
            temp1 = [obj1.a,  0; 0 obj1.b];
            temp2 = [obj2.a, 0; 0 obj2.b];
            temp3 = temp1*temp2;
            obj.a = temp3(1,1);
            obj.b = temp3(2,2);
        end
        function r = matrix(obj)
            r = [obj.a,  0; 0 obj.b];

        end
    end
end

I am trying to create a class for 2by2 diagonal matrix. I am having trouble with defining a method. I have, a method

    function r = matrix(obj)

This should convert my object to a diagonal matrix. But I get error when I try to use this method

a = diag2by2(1,2)

a =

diag2by2 with properties:

a: 1
b: 2

a.matrix() No appropriate method, property, or field matrix for class diag2by2.

Can someone help me with this?

Edit: whole class posted

Was it helpful?

Solution

You have matrix nested inside mtimes:

function obj = mtimes(obj1, obj2)
    temp1 = [obj1.a,  0; 0 obj1.b];
    temp2 = [obj2.a, 0; 0 obj2.b];
    temp3 = temp1*temp2;
    obj.a = temp3(1,1);
    obj.b = temp3(2,2);
function r = matrix(obj)
    r = [obj.a,  0; 0 obj.b];
end
end

EDIT: Did you just fix it? Your post no longer looks like the above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top