count ranks in specific row/column
2 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Tommy
on 21 Apr 2020
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
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!