Behavior of repmat with user-defined object

5 Ansichten (letzte 30 Tage)
Matt J
Matt J am 9 Mär. 2020
Kommentiert: dpb am 9 Mär. 2020
I have defined a class which displays, and in many ways behaves, like a table:
>> obj
obj =
A B
________ ________
Row1 [0.7943] [0.1656]
Row2 [0.3112] [0.6020]
Row3 [0.5285] [0.2630]
I have also defined a horzcat method for the class which autogenerates column names when needed to avoid duplicates:
>> [obj,obj]
ans =
A B Col3 Col4
________ ________ ________ ________
Row1 [0.7943] [0.1656] [0.7943] [0.1656]
Row2 [0.3112] [0.6020] [0.3112] [0.6020]
Row3 [0.5285] [0.2630] [0.5285] [0.2630]
I have not defined a repmat method for the class, and therefore I expected the code below to give an error, but it didn't. Instead, Matlab's built-in repmat produces this result,
>> repmat(obj,1,2)
ans =
A B A_1 B_1
________ ________ ________ ________
Row1 [0.7943] [0.1656] [0.7943] [0.1656]
Row2 [0.3112] [0.6020] [0.3112] [0.6020]
Row3 [0.5285] [0.2630] [0.5285] [0.2630]
which interestingly as well, is still a member of my user-defined class.
>> whos ans
Name Size Bytes Class Attributes
ans 3x4 3343 TabularCell
Note that the result is more or less what one gets if you first convert my object to a regular table, except that the result remains as type table,
>> repmat(table(obj),1,2)
ans =
3×4 table
A B A_1 B_1
________ ________ ________ ________
Row1 [0.7943] [0.1656] [0.7943] [0.1656]
Row2 [0.3112] [0.6020] [0.3112] [0.6020]
Row3 [0.5285] [0.2630] [0.5285] [0.2630]
So, going back to the case of repmat(obj,1,2), it appears that repmat automatically knew that I had a table() converter method defined and decided to use it to pre-convert my obj to a table. My question is, how did repmat know that my table() method had the required behavior, or does it just automatically try/catch all possible conversions it can think of until something works?
  1 Kommentar
dpb
dpb am 9 Mär. 2020
"how did repmat know ...?"
Access to the internal CrystalBall Toolbox, mayhaps?
As an old prof said regarding pair production, "And then magic happens!"

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by