Trying to count number of cells between two columns

2 visualizaciones (últimos 30 días)
Azairis
Azairis el 14 de Nov. de 2019
Respondida: Shubham Gupta el 15 de Nov. de 2019
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.

Respuestas (1)

Shubham Gupta
Shubham Gupta el 15 de Nov. de 2019
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

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by