number of elements
Mostrar comentarios más antiguos
I have an array A(1483 rows, 417 columns) with integer values of 1 to 5. I want to know whithin each block 92*92(i:i+91,j:j+91), what is the percentage of each value. I looked at size(), hist(), numel() but I don't think they can do what I want. Could you please help me?
Respuesta aceptada
Más respuestas (1)
Andrei Bobrov
el 28 de Abr. de 2011
variant the full solutions
A = randi([1 5], 1483,417);
m = 92;
mn = fix(size(A)/m);
Awork = A(1:mn(1)*m,1:mn(2)*m);
A1 = reshape(permute(reshape(Awork,m,mn(1),m,[]),[1 3 2 4]),m^2,[]);
A2 = arrayfun(@(x)histc(A1(:,x),1:5),1:prod(mn),'UniformOutput' , false);
Out = cell2mat(A2)/m^2;
more variant
A = randi([1 5], 1483,417);
m = 92;
mn = fix(size(A)/m);
Awork = A(1:mn(1)*m,1:mn(2)*m);
A1 = reshape(permute(reshape(Awork,m,mn(1),m,[]),[1 3 2 4]),m^2,[]);
Out = cell2mat(arrayfun(@(x)histc(A1(:,x),1:5)/nnz(A1(:,x)),1:prod(mn),'UniformOutput' , false));
6 comentarios
Walter Roberson
el 28 de Abr. de 2011
Hmmm, yes -- what to do about the boundaries? 417 certainly isn't a multiple of 92. Andrei's code stays sane by using a temporary matrix that is multiples of 92 in each direction.
Hassan
el 29 de Abr. de 2011
Hassan
el 29 de Abr. de 2011
Andrei Bobrov
el 29 de Abr. de 2011
understood, working
Walter Roberson
el 29 de Abr. de 2011
In my code, if you have some 0 values that you need to be ignored when calculating percentages, then instead of using
p = h ./ (92*92) * 100;
you should use
p = h ./ sum(h) * 100;
Note though that this will give a vector of nan if all the elements in the block are zero.
Hassan
el 29 de Abr. de 2011
Categorías
Más información sobre Component-Based Modeling en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!