Number of times two numbers appear together

2 次查看(过去 30 天)
Hi,
I have an array A = [1 3 2 4 3 4 3 2 1 1 3 2 4 3 3 2].
How can I count the number of time the number 2 occurs after 1, the number of times the number 3 occurs after 1, and the number of times the number 4 occurs after 1?
Any help would be appreciated.
Thanks,
DB

采纳的回答

Stephen23
Stephen23 2018-10-10
编辑:Stephen23 2018-10-10
>> A = [1,3,2,4,3,4,3,2,1,1,3,2,4,3,3,2];
Method one: basic indexing and nnz:
>> nnz(A(1:end-1)==1 & A(2:end)==2)
ans = 0
>> nnz(A(1:end-1)==1 & A(2:end)==3)
ans = 2
>> nnz(A(1:end-1)==1 & A(2:end)==4)
ans = 0
Method two: strfind:
>> nnz(strfind(char(A),char([1,2])))
ans = 0
>> nnz(strfind(char(A),char([1,3])))
ans = 2
>> nnz(strfind(char(A),char([1,4])))
ans = 0

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Elementary Math 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by