I would like to filter a array of objects (animal class) and get a new array of objects that fits in 2 properties conditions simultaneally.
Example:
I want to get all animals that have more than 5 years old and less than 10 inches of height.
My animal class is:
classdef animal
properties
age % years
height % inches
name % string
end
end
So I can create animals objects like
dog1 = animal();
dog1.age = 10;
dog1.height = 15;
dog1.name = "Bob";
cat1 = animal();
cat1.age = 2;
cat1.height = 10;
cat1.name = "Alice";
And than put all animals objects in same array, like:
my_array = {dog1, cat1, ...};
How can I "filter" my_array using 2 conditionals (age and height)?

 채택된 답변

Luna
Luna 2018년 11월 27일

0 개 추천

Hi Nycholas,
Try this code below. Filtered array will return to you Jessie and Kayle which meets the conditions.
dog1 = animal();
dog1.age = 10;
dog1.height = 15;
dog1.name = 'Bob';
cat1 = animal();
cat1.age = 2;
cat1.height = 10;
cat1.name = 'Alice';
dog2 = animal();
dog2.age = 10;
dog2.height = 8;
dog2.name = 'Jessie';
cat2 = animal();
cat2.age = 6;
cat2.height = 3;
cat2.name = 'Kayle';
my_array = {dog1, cat1,dog2,cat2};
idx = logical(zeros(1,numel(my_array)));
for i = 1:numel(my_array)
if my_array{i}.age > 5 && my_array{i}.height < 10
idx(i) = true;
end
end
filteredArray = my_array(idx);

댓글 수: 4

Thanks Luna!
This code could resolve my problem, but I'm looking for some optmizated way to do that, because I will have a lot of animals objects to process...like hundred thousand animals.
If I use de "for and if" commands, the overall performance will be slow.
May be, MATLAB could have some commands to filter objects in the array to optimazed speed, like this example below:
% Dummy data:
array_a = [1 2 3 4];
array_b = [5 6 7 8];
% Slow version: Bad performance
for i = 1:length(array_a)
result(i) = array_a(i) * array_b(i);
end
% Fast version: Best performance
result = array_a .* array_b;
Luna
Luna 2018년 11월 28일
편집: Luna 2018년 11월 28일
If you want to get that kind of operation, you should create a vector of ages and heights of your animals.
AgesVector = [10 5 3 6 20];
HeightsVector = [20 15 3 5 6];
idx = AgesVector > 5 & HeightsVector < 10;
filteredArray = my_array(idx);
How did you create your my_array? If it has a hundred thousand element, I assume it is not possible to create it with: my_array = {dog1, cat1, etc ... };
So maybe we can find an easy way to create vector of your animal's ages and heights. Creating table would help I guess.
Sure Luna, you are right!
In the end of the day, I'm thinking to use vectors (or a big matrix) to do these operations above.
I was just trying to implement the same idea using OOP, because, in my opinion, the code stays more "clear"...but, any way...
I put here the "animal class" just to abstract my real problem: Read/Import all musical notes from a digital music score/sheet and use MATLAB to analysis the music score in mathematics way...
So, my read/import process is a "for loop" statement that runs on each note of my music score file. Inside this "for loop" I was creating a "musical note" object from the class below:
classdef musical_note
properties
midi_note % Scalar value, like: 60
duration % in seconds, like: 0.5
bar % or measure, like: 1578
instrument % Instrumen who is playing this note, like: "Piano"
Other1 % Other parameter of the musical note...
Other2
Other3
OtherN
end
end
After the import process, I will have the 'my_array' filled with all music_notes object.
Now, I would like to filter this array, in some "magical way", like:
filtered_array = some_function(my_array, midi_note = 50, duration > 10, instrument = "Piano")
As you well said, I think that the best way to implement this is creanting a big matrix and not use OOP. Right?
Thank you for your quickly answer!
Luna
Luna 2018년 11월 29일
편집: Luna 2018년 11월 29일
It does nothing to do with OOP, I think what you need is a kind of table structure for that magical way below.
filtered_array = some_function(my_array, midi_note = 50, duration > 10, instrument = "Piano")
You should set a good defined tables for what you need or be using a database.
(You can still continue to use OOP)

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

추가 답변 (1개)

Matt J
Matt J 2018년 11월 29일
편집: Matt J 2018년 11월 29일

1 개 추천

In this scenario, it is better to have a single object whose properties are arrays than an array of objects whose properties are scalars. However, the following should be faster than looping,
my_array = [my_array{:}]; %get rid of cell array storage
conditions = [my_array.age]>5 & [my_array.height]<10;
filtered_array = my_array(conditions);

제품

릴리스

R2018b

질문:

2018년 11월 27일

편집:

2018년 11월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by