Counting how many times a number occured after a specific number
Show older comments
Lets say I have a sequence of numbers ranging from 1 to 4, S = [ 3 2 2 4 3 1 ]. I want a 4x4 matrix which tells me how many times I went from say 3 to 2 or 4 to 1. It would look like this: M = [ 0 0 0 0; 0 1 0 1; 1 1 0 0; 0 0 1 0]. Sounds simple, but I'm out of thoughts. Thanks.
1 Comment
darova
on 10 Apr 2020
Can you explain more? I don't understand
Accepted Answer
More Answers (2)
Andrei Bobrov
on 10 Apr 2020
accumarray(hankel(S(1:end-1),S(end-1:end)),1)
AB WAHEED LONE
on 14 Dec 2021
0 votes
What about m*n matrix
for example , S= [1 2 2 3 1;
1 3 1 3 4;
1 3 4 1 3;
1 3 4 1 3;
1 3 4 1 3];
When i tried to count the same ((1,1),(1,2),(1,3),(1,4),(2,2),(2,3), etc), it just creates the square matrix of max element size.
for example in case of first row X=[1 2 2 3 1], the size of output matrix is 3*3 ,which should have been 4*4.
could you comment on this.
Categories
Find more on Logical 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!