I would like to remove rows and columns which are empty (contains more zeros than value) : for example
A=[0 0 0 0; 0 1 2 0; 8 5 8 2;5 8 2 7;0 1 0 0; 0 0 0 0]
to give output as
B=[5 8; 8 2]

 採用された回答

Walter Roberson
Walter Roberson 2018 年 3 月 22 日

1 投票

nz = logical(A);
B = A(sum(nz,2) >= size(A,2)/2, sum(nz) >= size(A,1)/2);
You will notice this gives a 3 x 2 array, not a 2 x 2 array. The row 0 1 2 0 does not have more 0 than values, so it needs to be kept.
If you change the rules to say that you are not to keep something unless the number of values exceeds 1/2 of the possible, then you need to eliminate the third column of A, [0; 2; 8; 2; 0; 0] because in that case 3 entries out of 6 are populated and 3 does not exceed (6/2)

6 件のコメント

Swapnil Rane
Swapnil Rane 2018 年 3 月 22 日
That won't be a problem, the matrix i'm working is huge in size Thank you for the answer
Swapnil Rane
Swapnil Rane 2018 年 3 月 22 日
Can I get the position of the four corner elements? where the matrix was extracted from.
Walter Roberson
Walter Roberson 2018 年 3 月 22 日
nz = logical(A);
wanted_rows = find(sum(nz,2) >= size(A,2)/2);
wanted_cols = sum(nz) >= size(A,1)/2;
B = A(wanted_rows, wanted_cols);
You could look at min(wanted_rows), max(wanted_rows) and min(wanted_cols) and max(wanted_cols), but doing so would imply that the selected items were consecutive, which is not something that your original question permits us to assume.
Swapnil Rane
Swapnil Rane 2018 年 3 月 22 日
編集済み: Swapnil Rane 2018 年 3 月 22 日
The matrix, I'm working on is 190*97, So, I just need the four corner points..... min(wanted cols) and max(wanted_cols) give logical variable. I'm confused.
Walter Roberson
Walter Roberson 2018 年 3 月 22 日
wanted_cols = find(sum(nz) >= size(A,1)/2);
Swapnil Rane
Swapnil Rane 2018 年 3 月 22 日
Thank you!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2018 年 3 月 22 日

1 投票

Here is one way:
A=[0 0 0 0; 0 1 2 0; 8 5 8 2;5 8 2 7;0 1 0 0; 0 0 0 0]
B=[5 8; 8 2]
[rows, columns] = size(A)
% Count the number of zeros in each row.
zerosPerRow = sum(A == 0, 2)
% Count the number of zeros in each column.
zerosPerColumn = sum(A == 0, 1)
% Determine which rows and columns to keep
rowsToKeep = zerosPerRow <= columns/2
columnsToKeep = zerosPerColumn <= rows/2
% Extract the rows and columns that we want:
B = A(rowsToKeep, columnsToKeep)
It's fairly explicit, with lots of comments, so that you can easily understand it and see all the steps. If you want a more compact, but more cryptic and harder to understand, one liner, then wait and I'm sure someone will post it.

3 件のコメント

Swapnil Rane
Swapnil Rane 2018 年 3 月 22 日
WORKS FINE THANK YOU
Swapnil Rane
Swapnil Rane 2018 年 3 月 22 日
Can I get the position of the four corner elements? where the matrix was extracted from.
Image Analyst
Image Analyst 2018 年 3 月 22 日
topRow = find(rowsToKeep, 1, 'first');
bottomRow = find(rowsToKeep, 1, 'last');
leftColumn = find(columnsToKeep , 1, 'first');
rightColumn = find(columnsToKeep , 1, 'last');

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by