Assuming I have 1 x 20 structure arrays, how can obtain the ones that are decreasing element wise?

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

Are these really structure arrays, or are they actually numeric arrays?
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.
I am sorry for confusion. I have this data which is 1 x 133 struct array with fields f1, f2, and f3. I am only interested in f1 (data.f1), and each array in the first field (f1) is 200 x 1. (For example, data(1).f1 has 200 elements)
Sorry if it is confusing as I have just started learning Matlab. Thank you.
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.

Sign in to comment.

 Accepted Answer

It is not very clear what you are trying to achieve. Here are some tools that you might find useful. The function that you should have a play around with is diff: you can use it to locate and identify runs of monotonic increasing/decreasing element values.
To identify monotonic strictly decreasing vectors:
>> vec_dec = [5,4,3,2];
>> all(diff(vec_dec)<0)
ans =
1
To identify monotonic strictly increasing vectors:
>> vec_inc = [1,2,3,4];
>> all(diff(vec_inc)>0)
ans =
1
To extract monotonic decreasing elements from a vector:
>> vec_mix = [1,2,3,4,3,2,1];
>> idx = diff(vec_mix)<0;
>> vec_mix([false,idx] | [idx,false])
ans =
4 3 2 1

More Answers (1)

f1_decreases = arrayfun(@(K) YourStruct(K).f1(1) > YourStruct(K).f1(2), 1:length(YourStruct));
kept_Struct = YourStruct(f1_decreases);

Categories

Products

Asked:

on 18 May 2015

Edited:

on 18 May 2015

Community Treasure Hunt

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

Start Hunting!