Deleting row and column with all zeros and putting it back

3 ビュー (過去 30 日間)
Mohammad Asif Iqbal Khan
Mohammad Asif Iqbal Khan 2018 年 3 月 23 日
編集済み: Matt J 2018 年 3 月 23 日
Hell0,
I am trying to perform an operation on a big Matrix (within a cell). Here,
B=
-17.5310 +20.7302i 0.0000 + 0.0000i 5.4364 - 2.0952i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
5.4364 - 2.0952i 0.0000 + 0.0000i -17.5310 +20.7302i
so, I used this,
data=B{1,1};
data( ~any(data,2), : ) = []; %rows
data( :, ~any(data,1) ) = []; %columns
data2=data;
This deletes the rows and columns with all zeros and returns me this,
data2=
-17.5310 +20.7302i 5.4364 - 2.0952i
5.4364 - 2.0952i -17.5310 +20.7302i
then, I inverse the matrix and get my desired matrix. But, after that, I want to put back the deleted rows and column to its original places. How can I do that?

採用された回答

Matt J
Matt J 2018 年 3 月 23 日
編集済み: Matt J 2018 年 3 月 23 日
Another solution would be to never remove them in the first place
data=B{1,1}; data2=data;
r=any(data,2);
c=any(data,1);
data2(r,c)=inv(data(r,c));

その他の回答 (2 件)

Rik
Rik 2018 年 3 月 23 日
B={[-17.5310+20.7302i 0.0000+0.0000i 5.4364-2.0952i
0.0000+0.0000i 0.0000+0.0000i 0.0000+0.0000i
5.4364-2.0952i 0.0000+0.0000i -17.5310+20.7302i]};
data=B{1,1};
keep_rows=any(data,2);
data( ~any(data,2), : ) = []; %rows
keep_cols=any(data,1);
data( :, ~any(data,1) ) = []; %columns
data2=data;
%do whatever you need to do with the data2 matrix
data3a=zeros(size(data2,1),numel(keep_cols));
data3a(:,keep_cols)=data2;%undo removing of cols
data3=zeros(numel(keep_rows),numel(keep_cols));
data3(keep_rows,:)=data3a;%undo removing of rows

Birdman
Birdman 2018 年 3 月 23 日
編集済み: Birdman 2018 年 3 月 23 日
One workaround could be(Consider your B is double array, extracted from the cell):
r=all(B==0,2);
c=all(B==0,1);
B(r,:)=[];
B(c,:)=[];
%do your calculation
B(:,find(c)+1)=B(:,c)
B(find(r)+1,:)=B(r,:)
B(:,c)=0+0j;
B(r,:)=0+0j

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by