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

 採用された回答

Star Strider
Star Strider 2014 年 5 月 22 日

2 投票

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 件のコメント

Uet
Uet 2014 年 5 月 23 日
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.
Star Strider
Star Strider 2014 年 5 月 23 日
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 2014 年 5 月 25 日
Thank you very much... it works perfect.
Star Strider
Star Strider 2014 年 5 月 25 日
My pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Romain
Romain 2014 年 5 月 21 日

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 件のコメント

Uet
Uet 2014 年 5 月 22 日
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.

サインインしてコメントする。

カテゴリ

製品

質問済み:

Uet
2014 年 5 月 21 日

コメント済み:

2014 年 5 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by