Trying to count number of cells between two columns

조회 수: 1 (최근 30일)
Azairis
Azairis 2019년 11월 14일
답변: Shubham Gupta 2019년 11월 15일
Hi, just a noob student here trying to figure out a way to count the number of cells between two columns:
%count the times 0's happened, then delete
Distances = raw(:, 5:6); %The number of cells in columns 5 and 6 are a total of 6
whichDistance = cell2mat(Distances)==0;
nD0 = sum(whichDistance);
if nD0 >0
fprintf('\nThis team had %d distances that did not count.\n',nD0);
end
raw(whichDistance, :)=[];
nValDis = size(raw,1) %But nValDis says its only 3
The objective here is that Im trying to take out the 0s out of the cells if there is any. In these columns there isnt any 0s so the size should be 6 valid distances.
Thanks for the help! I'd appreciate it.

답변 (1개)

Shubham Gupta
Shubham Gupta 2019년 11월 15일
In the code that you have shared whichDistance is an empty matrix. So when you do
raw(whichDistance, :)=[];
raw array doesn't change and when you do
nValDis = size(raw,1)
It returns number of rows in raw array which is 3 I suppose.
To achieve what you need, you should count 1s and 0s in the whichDistance matrix. One of the way could be:
whichDistance = cell2mat(Distances)==0;
nD0 = sum(sum(whichDistance)); % Number of 1s, sum(sum()) to calculate total sum of matrix
if nD0 >0
fprintf('\nThis team had %d distances that did not count.\n',nD0);
end
nValDis = numel(whichDistance)-nD0 % total number of elemets - element with 1s = element with 0s

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by