필터 지우기
필터 지우기

How to properly overload horzcat and vertcat?

조회 수: 7 (최근 30일)
Eugenio Grabovic
Eugenio Grabovic 2019년 1월 24일
편집: per isakson 2019년 1월 25일
Hi, im having some trouble overloading horzcat/vertcat for my class with 2 properties:
classdef myClass
properties
a
b
end
methods
function obj = myClass(u,v)
obj.a = u;
obj.b = v;
end
function out = horzcat(varargin)
...
end
end
end
As a result of the concatenation i want to achieve just the concatenations of the two proprieties, for example concatenating those objects:
x = myClass(2,3);
y = myClass(4,5);
ConcArray = [x,y];
i would like to have as a result a single object with as properties the concatenation of each property. Basically having the equivalent of:
EquivalentObj = myClass([2,4],[3,5])
I came up with this solution:
function out = horzcat(varargin)
n = length(varargin);
aProp = [];
bProp = [];
for i = 1 : n
var = varargin{i};
aProp = [aProp var.a];
bProp = [bProp var.b];
end
out = myClass(aProp,bProp);
end
but it looks really bad since i couldn't preallocate the concatenating arrays, im using a for loop and in each loop im using standard horzcat recursively.
Any ideas on how to improve that ? Thank you in advance.
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 1월 25일
I do not see any property data type validation in your constructor, so we must assume that you want to be able to handle inputs of different data types, like
[myClass(2,3), myClass(11, tf([17 0], [1 9]))]
But to handle this you are going to have to define what the output should be. Your question suggests that you would want property a to become [2 11], but [3 tf([17 0], [1 9]))] cannot be directly mixed, and we don't know what you want to have happen in this situation.
Eugenio Grabovic
Eugenio Grabovic 2019년 1월 25일
Yeah sorry i didn't explained myself correctly: the property data type validation is implemented already, my main focus was on how to properly ( in a performant way) to overload concatenations assuming i always concatenate the same data types properties.
Mr. Isakson showed me what i needed.

댓글을 달려면 로그인하십시오.

채택된 답변

per isakson
per isakson 2019년 1월 25일
편집: per isakson 2019년 1월 25일
This looks more Matlabish, but whether it's better ...
classdef myClass
properties
a@double
b@double
end
methods
function obj = myClass(u,v)
obj.a = u;
obj.b = v;
end
function out = horzcat(varargin)
obj = cat( 2, varargin{:} );
as = [ obj.a ];
bs = [ obj.b ];
out = myClass( as, bs );
end
end
end
Test
%%
x = myClass(2,3);
y = myClass(4,5);
ConcArray = [x,y]
outputs
ConcArray =
myClass with properties:
a: [2 4]
b: [3 5]
>>
and try @Walter's expression
>> [myClass(2,3), myClass(11, tf([17 0], [1 9]))]
Error setting property 'b' of class 'myClass':
Value must be 'double'.
Error in myClass (line 9)
obj.b = v;
"I couldn't preallocate" why not just
n = length(varargin);
aProp = nan(1,n);
bProb = nan(1,n);
  댓글 수: 2
Eugenio Grabovic
Eugenio Grabovic 2019년 1월 25일
편집: Eugenio Grabovic 2019년 1월 25일
That's exactly what i was looking for! Thank you.
"aProp = nan(1,n);"
"bProb = nan(1,n);"
Actually its like preallocating with zeroes or ones, problem is i couldn't just "add" columns
aProp = [aProp var.a];
concatenating previous ones like this. Anyway it doesn't matter since you overload suggestion is just fine as it is.
per isakson
per isakson 2019년 1월 25일
편집: per isakson 2019년 1월 25일
I use NaN when preallocating, because if I fail to overwrite all preallocated values NaN will mostly likely quicker make me aware of the problem. Zero and one are often legal values.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by