Occurence with 2 variables

4 views (last 30 days)
Bossennec Guillaume
Bossennec Guillaume on 12 Jul 2019
Hello,
I have a little problem, I do not know how to calculate a occurence of couple of value.
I explain,
I have some waves datas (3600) with height and direction.
HsW=[2 5 1 3]; %height
DirW=[42 24 35 45];% direction
for exemple : I want to calculate the occurence of the couple 2 42 ,5 24 in my datas.
I have seen some matlab function like tabulate but it's always for vector, not for array.
Is there some functions or code that's can do that.
Hoping that you can help me,

Accepted Answer

dpb
dpb on 13 Jul 2019
HD=[HsW;DirW].'; % combine data for convenience
Pattern=[HD(1:2,:)]; % the looked for pattern
[~,l2]=ismember(HD,Pattern,'rows'); % locations where pattern was found
N=sum(l2((l2==1)+1)==2); % count locations of L1 followed by L2
Above doesn't account for (assumed rare) possibility that the pattern line 1 may be the last element in the array. If that case were to occur, augment the l2 vector with a NaN or any nonmatching value to avoid the bounds error from the +1 address expression.
The locations of the match are simply
find(l2((l2==1)+1)==2)
It's often simpler to turn into strings for pattern matching as then one can make simply a byte comparison of vector or array of characters
  1 Comment
Bossennec Guillaume
Bossennec Guillaume on 15 Jul 2019
Ok thanks for your answer,
that's help me,
I will try with string to improve my skills,

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!