Logical indexing a 2D array into a 2D array

62 ビュー (過去 30 日間)
Eric Smith
Eric Smith 2018 年 4 月 19 日
回答済み: Wouter Verstraelen 2021 年 4 月 19 日

I am having trouble finding a way to remove all rows and columns that are all zeros from a 2D matrix to get a resulting 2D matrix using logical indexing.

Z = [56,0,0,0,0,55;
      0,0,0,0,0,0;
     27,0,0,0,0,0;
      0,0,0,0,0,0;
      0,0,0,0,0,0;
     100,0,0,0,0,25];
zidx = Z ~= 0;
Z2 = Z(zidx);

results in column vector of:

Z2 = [56;27;100;55;25]

However I really need it to be:

Z2 = [56,55; 27,0; 100,25];

Any help is greatly appreciated.

採用された回答

Jan
Jan 2018 年 4 月 19 日
編集済み: Jan 2018 年 4 月 19 日

Easier and faster without a loop:

Z = [56,0,0,0,0,55;
      0,0,0,0,0,0;
     27,0,0,0,0,0;
      0,0,0,0,0,0;
      0,0,0,0,0,0;
    100,0,0,0,0,25];
Z = Z(:, any(Z, 1));  % Keep only columns with any non-zero value
Z = Z(any(Z, 2), :);  % Keep only rows with any non-zero value

If you really want Z = [56,27,100; 55,0,25] as output, append:

Z = Z.'
  2 件のコメント
Eric Smith
Eric Smith 2018 年 4 月 19 日
Thanks. This works and is closer to what I was thinking as a solution.
Guillaume
Guillaume 2018 年 4 月 19 日
is closer to what I was thinking as a solution
This is the best solution!

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

その他の回答 (2 件)

Wouter Verstraelen
Wouter Verstraelen 2021 年 4 月 19 日
there's actually a very short to do this that hasn't been mentioned so far
Z =Z.*(Z~= 0)

Bob Thompson
Bob Thompson 2018 年 4 月 19 日

So, to clarify, you want to have your columns turned into rows?

Because you are looking to roughly keep the row and column locations I would suggest using a for loop to keep the non-zero values from each row. Before that though, because you have different numbers of values you're going to need to either initialize the output array as the max size in zeroes, or to pad as you add new rows.

Z2 = [];
for k = 1:size(Z,1); % Easier to do row wise because of needing to pad rows, rather than columns.
 row = Z(k,Z(k,:)~=0);
 if isempty(row);  
 elseif isempty(Z2);
  Z2 = row;
 elseif size(Z2,2)<length(row); % Check if previous row was smaller than new row
  Z2(:,size(Z2,2)+1:length(row)) = 0;
  Z2 = vertcat(Z2,row);
 elseif size(Z2,2)>length(row); % Check if previous row was bigger than new row
  row(length(row)+1:size(Z2,2)) = 0;
  Z2 = vertcat(Z2,row);
 else
  Z2 = vertcat(Z2,row);
 end
end
Z2 = Z2';
  1 件のコメント
Eric Smith
Eric Smith 2018 年 4 月 19 日
編集済み: Eric Smith 2018 年 4 月 19 日
This works, and sorry that I mistyped column major for output matrix... output should be: Z = [56,27,100; 55,0,25];

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by