Assuming I have 1 x 20 structure arrays, how can obtain the ones that are decreasing element wise?
Show older comments
As mentioned in the title, I have couple of structure arrays, that some of them are
- Increasing, such that [a1, a2, a3......] where a1<a2
- Decreasing, such that [b1, b2, b3......] where b1>b2.
I am only interested in the decreasing arrays and would like to filter out the unnecessary ones. I was thinking of using the function strcmp(), but I am not sure how and where to use it. Please help. Thanks!
4 Comments
That description them makes it seem like that they are numeric arrays, which are a very different thing from structure arrays. For a start structure arrays do not have any inherent mathematical definition of equality or inequality, so the statement "that some of them are increasing" does not make sense for structures.
bio lim
on 18 May 2015
Then you need to read this:
And in particular you might find this syntax very useful ( data is the structure variable name):
>> [data.field]
is equivalent to
>> [data(1).field, data(2).field,..., data(end).field]
and ditto for a cell array and function calls:
>> {data.field} % creates a cell array
>> some_function(data.field)
This works because for a structure the syntax structure.fieldname creates a comma-separated list, which can be used in many situations.
bio lim
on 18 May 2015
Accepted Answer
More Answers (1)
Walter Roberson
on 18 May 2015
f1_decreases = arrayfun(@(K) YourStruct(K).f1(1) > YourStruct(K).f1(2), 1:length(YourStruct));
kept_Struct = YourStruct(f1_decreases);
Categories
Find more on Data Type Identification in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!