MATLAB: Count ranks in specific row/column

#array #countMATLAB

I have a PxQxN matrix.
I need to count the number of non zero cells in each cell PQ as some of them have n<N due to incomplete test data.
I need it to produce another PxQ array of counts n for each PQ cell. I have already preallocated this matrix, I just need to refll the data
Any help would be appreciated.

Best Answer

Is something like this what you mean?
% sample PxQxN array:
P = 5; Q = 6; N = 10;
A = num2cell(randi([1 10], P, Q, N));
% set 15 random spots to 0:
for i = 1:15
A{randi(P), randi(Q), randi(N)} = 0;
end
% count number of nonzero elements in each A(P,Q,:):
C = sum(cellfun(@(c) c~=0, A), 3)
C =
8 9 10 8 9 10
10 10 10 8 10 10
10 10 10 9 10 9
10 10 9 10 10 9
9 8 10 10 10 10