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

5 views (last 30 days)
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
Stephen23
Stephen23 on 18 May 2015
Edited: Stephen23 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.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 18 May 2015
Edited: Stephen23 on 18 May 2015
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)

Walter Roberson
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 Loops and Conditional Statements 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!