How can i find rows or coulmns which contain maximum zeroes?

Hi, I want to select rows or coulmns which contain maximum zeros? can anyone please help me how to write a script file for this.. I want to develop a simple program for Assignment method..
Reagards,
Ateeq

 Respuesta aceptada

This will give you the number of zeros in particular rows and columns:
M = [105 0 55 15; 15 0 25 75; 55 0 0 10; 0 0 5 0];
[zr,zc] = find(M == 0);
Z = accumarray([zr zc], 1 )
rowzeros = sum(Z,2)
colzeros = sum(Z,1)
produces:
rowzeros =
1
1
2
3
colzeros =
1 4 1 1

4 comentarios

Uet
Uet el 23 de Mayo de 2014
Thank you very much.. Please help me in how to subtract 15 (which is minimum of all uncovered numbers) from all uncovered numbers. and add 15 to intersection of both lines as in 3rd, 4rth row and 2nd column as in picture. I will be much obliged.
Add after my previous code:
uncr = find(rowzeros < 2); % Find uncovered rows
uncc = find(colzeros < 2); % Find uncovered cols
Munc = M(uncr,uncc); % Uncovered rows & cols (diagnostic)
cvrr = find(rowzeros >= 2); % Find covered rows
cvrc = find(colzeros >= 2); % Find covered cols
M(cvrr,:) = M(cvrr,:) + 15; % Add 15 to covered rows
q = find(M(:,cvrc)<15); % Test for intersections
M(q,cvrc) = M(q,cvrc) + 15; % Add 15 to the others
M(uncr,uncc) = M(uncr,uncc) - 15 % Subtract 15 from the uncovered areas
I calculate Munc to be sure I have identified those correctly. The q line was necessary because of the intersections of column 2 and rows 3 and 4. I had to be sure I didn’t add 15 to elements I had already added it to in the previous lines. I couldn’t get logical indexing or arrayfun to work here, because I am only operating on some elements of the matrix and not the entire matrix.
I can’t promise that this will be robust for all such situations, but it works here. Have fun with it!
Uet
Uet el 25 de Mayo de 2014
Thank you very much... it works perfect.
My pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

If you want the row and the colum with the maximum of values equal to zeros, I propose (for a 2D matrix) :
[numberOfZeroCol,colWithMaxZero] = max(sum(array == 0, 1))
[numberOfZeroRow,rowWithMaxZero] = max(sum(array == 0, 2))
but I'm not sure that's what you want.

1 comentario

Uet
Uet el 22 de Mayo de 2014
Actually I want to find number of rows and coulumns which contain more than two zeroes as covered by color lines in given picture. Then subtract 15 (which is minimum of all uncovered numbers) from all uncovered numbers.

Iniciar sesión para comentar.

Categorías

Productos

Preguntada:

Uet
el 21 de Mayo de 2014

Comentada:

el 25 de Mayo de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by