How to count non-zero elements in a column but over a specified range?
조회 수: 2 (최근 30일)
이전 댓글 표시
I'm trying to get the sum of non-zero elements in a column and store these values. The column consists of a list of numbers that range from 1 to 5000 and then repeats again 1 to 5000. I'm trying to count the number of non-zero elements in one range (from 1 to 5000), store this value, and then move onto the next range of 1 to 5000, store this value etc.
I've tried the following:
store_sum = zeros(length(list_values));
for i = 1:length(list_values)
for j = 0:5000
store_sum = sum( i ~= 0,1);
end
end
댓글 수: 1
Walter Roberson
2018년 1월 29일
Are the cycles always 5000 apart -- the full 1:5000 but with some values replaced by 0? Or is it the case that you have a sorted subset of 1:5000 except that the sorted subset might have 0's in it?
채택된 답변
Matt J
2018년 1월 29일
편집: Matt J
2018년 1월 29일
This may be what you want, but your description of the problem is very hard to interpret. You just want the number of non-zero elements in each column of true_spike?
store_sum = sum(true_spike>0)
댓글 수: 2
Image Analyst
2018년 1월 29일
What is true_spike? He doesn't mention it, at least in the current form of his question.
I can't figure out what "repeats" means. Let's say that column has 100 elements with values in the range 0-5000. But then what does repeat mean? Like rows 101 to 200 or whatever also are in the range 0 to 5000? How are we supposed to know where these ranges start and stop? You just took the sum over the whole column, which is what I would have done, given the lack of information. I guess it was correct because he accepted it.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!