Why can an empty array constructed from a built-in class perform multiplication, while an empty array generated from my custom class cannot perform multiplication?
1 view (last 30 days)
Show older comments
Here is the example
It is work.
A=double.empty(5,0);
B=double.empty(0,5);
C=A*B
C =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
It is can not work:
classdef ColorInRGB<handle
properties
Color;
end
end
A=ColorInRGB.empty(5,0);
B=ColorInRGB.empty(0,5);
C=A*B
'ColorInRGB' 类型的操作数不支持运算符 '*'。
0 Comments
Accepted Answer
recent works
on 29 Aug 2023
you are observing is due to how operators are defined and implemented for different classes. The behavior of operators, such as multiplication (*), is determined by the class's implementation and the operations it supports.
In the first example, where you're using the built-in double class, MATLAB has predefined behaviors for numeric types like double. When you multiply two empty matrices of type double, MATLAB interprets it as matrix multiplication, and the result is an empty matrix of the appropriate size. This is why you're seeing a matrix of zeros in your result C.
However, in the second example, you've defined a custom class ColorInRGB. Since this is your own class, you have to explicitly define how the multiplication operator should behave for objects of this class. By default, MATLAB doesn't know how to perform matrix multiplication for instances of your custom class unless you define the multiplication behavior for that class.
The error message 'ColorInRGB' 类型的操作数不支持运算符 '*'。 translates to "Operator '*' is not supported for operands of type 'ColorInRGB'."
If you want to perform multiplication for instances of your custom class, you'll need to define the multiplication operation for the ColorInRGB class. You can do this by overloading the mtimes method in your class definition. This method will define how instances of your class should be multiplied. Here's an example of how you might implement the mtimes method for your ColorInRGB class:
classdef ColorInRGB < handle
properties
Color;
end
methods
function obj = ColorInRGB(initialColor)
if nargin > 0
obj.Color = initialColor;
else
obj.Color = [0, 0, 0]; % Default color
end
end
function result = mtimes(obj1, obj2)
% Define how multiplication should work for ColorInRGB objects
result = ColorInRGB();
% Perform your custom multiplication logic here
end
end
end
In this example, you would need to fill in the actual multiplication logic inside the mtimes method to achieve the behavior you desire when multiplying ColorInRGB objects.
2 Comments
recent works
on 29 Aug 2023
you can use the methods function with the 'all' option to display all methods supported by an object, including hidden methods. Here's how you can use it
A = double.empty(5, 0);
methods(A, 'all')
The 'all' option tells MATLAB to display all methods, including those that are hidden. This should give you a complete list of methods supported by the object A, including both visible and hidden methods.
More Answers (0)
See Also
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!