필터 지우기
필터 지우기

count the number of transitions from 0 to 3 in cells in cell array

조회 수: 1 (최근 30일)
merialu
merialu 2018년 11월 13일
편집: merialu 2018년 11월 13일
Hi.
I have a cell array containing 1000 cells. in each cell there is a matrix with different number of rows but with 50 columns.
the rows contains number from 0-3. I want to count how many times a 0 is followed by a 3 in each column.
Below you can see the code that I used to count the total number of transitions, from any number to another. Is there an easy way to change this code so that it counts the number of transitions from a certain number to another?
x=numel(CA)
newCA=cell(x,1);
for i=1:x
newCA{i}=sum(diff(CA{i})~=0);
end

채택된 답변

Stephen23
Stephen23 2018년 11월 13일
편집: Stephen23 2018년 11월 13일
Here is a simple method based on logcal arrays, demonstrated on two Nx4 matrices in a cell array. The first matrix has two 0,3 transitions, the second matrix has three.
>> C = {[0,1,2,3;0,3,0,3;0,2,0,1],[0,3,1,2;3,2,1,0;3,0,3,0;3,0,3,3]};
>> C{:}
ans =
0 1 2 3
0 3 0 3
0 2 0 1
ans =
0 3 1 2
3 2 1 0
3 0 3 0
3 0 3 3
>> F = @(m) nnz(m(:,1:end-1)==0 & m(:,2:end)==3);
>> V = cellfun(F,C)
V =
2 3
>> N = sum(V)
N = 5
  댓글 수: 3
Stephen23
Stephen23 2018년 11월 13일
>> C = {[0,0,0,0;3,1,2,0;0,0,3,1;3,3,0,1],[1,1,0,3;0,3,3,3;3,3,3,0]};
>> C{:}
ans =
0 0 0 0
3 1 2 0
0 0 3 1
3 3 0 1
ans =
1 1 0 3
0 3 3 3
3 3 3 0
>> F = @(m) sum(m(1:end-1,:)==0 & m(2:end,:)==3, 1);
>> D = cellfun(F,C,'uni',0);
>> M = vertcat(D{:}) % for each matrix
M =
2 1 0 0
1 0 1 0
>> sum(M,1) % for all matrices
ans =
3 1 1 0
merialu
merialu 2018년 11월 13일
Yes, perfect!
Thank you for your help, really appriciate it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by