count number of indices
조회 수: 58 (최근 30일)
이전 댓글 표시
I have an array with 7 columns filled with data. Column 2 has 0s then a 1 then 0s and then a -1 in repeating order. The number of 0s between a 1 and a -1 varies.
In column 2 I want to count for each cell which has the value 1, the number of cells there are until the next cell with value -1. If the count is bigger than 100, I want to add the index of the cell with value 1, to the first available cell in column 8.
I have come furthest with the following code:
n=1;
for i = 1:length(arrayA)
if arrayA(i,2) == 1
if abs(find(arrayA(i,2)==1) - find(arrayA(:,2)==-1)) > 200
arrayA(n,8)=i;
n=n+1;
end
end
end
yet column 8 still doesn't have any values when I run the whole thing.
Pretty sure the problem is line 3 but have no more ideas left on how I could make it work...
댓글 수: 2
Rik
2020년 8월 6일
Where do you define n? Also, find might return multiple values, in which case you have a logical array for you if statement, which I always have to look up what it does. Are you sure you implemented that line correctly?
채택된 답변
gummiyummi
2020년 8월 7일
댓글 수: 2
Rik
2020년 8월 7일
If the number of elements in I and J are equal, you can use find to remove the loop.
추가 답변 (1개)
Matt J
2020년 8월 6일
편집: Matt J
2020년 8월 6일
Using group1s from the File Exchange
col2=arrayA(:,2);
I=find(col2==1);
H=histcounts(nonzeros(group1s(~abs(col2))));
loc=I(H(2:2:end)>100);
arrayA(loc,8)=loc;
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!