- the number 20
- the numbers 7 and 13
- the values 1 and 2 of the property a
Compare classes with different Properties
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Lucas Russi
am 4 Nov. 2019
Kommentiert: Lucas Russi
am 9 Nov. 2019
Hi
Is there an easy way compare classes of the same type with variable attributes ?
I have an array which contains instances of a class
example would be something like this
instance 1
classdef MyClass
properties
a = 1;
end
methods
end
end
Instance 2
classdef MyClass
properties
a = 2;
end
methods
end
end
So lets say I have an array of 20 instances of MyClass - 7 of them would have a = 1, 13 of them would have a = 2
The desired output would be something like
Output = [7 1;13 2]
Thanks alot - Help is much appreciated
0 Kommentare
Akzeptierte Antwort
per isakson
am 6 Nov. 2019
Try this
%% Create sample data
mc(20) = MyClass;
[mc(1:7).a] = deal(1);
[mc(8:20).a] = deal(2);
%% Desired output
edges = (1:2+1);
hc = histcounts( [mc.a], edges );
Output = [ reshape(hc,[],1), reshape(edges(1:2),[],1) ]
which shall output
ans =
7 1
13 2
This piece of code contains many "magic" numbers
the value of edges must match the values of the property a
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!