Trying to count number of cells between two columns
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!