Borrar filtros
Borrar filtros

count ranks in specific row/column

2 visualizaciones (últimos 30 días)
Brian Dowling
Brian Dowling el 21 de Abr. de 2020
Comentada: Brian Dowling el 21 de Abr. de 2020
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.

Respuesta aceptada

Tommy
Tommy el 21 de Abr. de 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
  1 comentario
Brian Dowling
Brian Dowling el 21 de Abr. de 2020
Worked a charm. Thanks Tommy. Been fighting with that one for ages!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by