フィルターのクリア

storing multiple matrices from a loop

1 回表示 (過去 30 日間)
Luqmann Mahama
Luqmann Mahama 2020 年 6 月 2 日
コメント済み: Luqmann Mahama 2020 年 6 月 2 日
I have a 5x5 matrix and what the program is supposed to do is to run through the columns(i) and whenever it finds a row in the ith column that is zero. It makes the whole row zero [0 0 0 0 0]. And store the different matrices for every(i) column in a matrix DBIBC(i). So far the loop only runs for the DBIBC(1) and stores only that in the array.
rw finds the rows in the column that are zero.
Please help me.
BIBC = [1 1 1 1 1;0 1 1 1 1;0 0 1 1 0;0 0 0 1 0;0 0 0 0 1] ;
DBIBC = BIBC;
N=length(BIBC);
for i=1:N
rw = find(DBIBC(:,i)==0)
DBIBC(rw,:)= 0
DBIBC(i)
B{i} = DBIBC;
end
  2 件のコメント
KSSV
KSSV 2020 年 6 月 2 日
Can you show us the epxected output for the above?
Luqmann Mahama
Luqmann Mahama 2020 年 6 月 2 日
DBIBC(1)=[1 1 1 1 1; 0 0 0 0 0; 0 0 0 0 0 ; 0 0 0 0 0 ; 0 0 0 0 0]
DBIBC(2)=[1 1 1 1 1; 0 1 1 1 1; 0 0 0 0 0 ; 0 0 0 0 0 ; 0 0 0 0 0]
DBIBC(3)=[1 1 1 1 1; 0 1 1 1 1; 0 0 1 1 0 ; 0 0 0 0 0 ; 0 0 0 0 0]
DBIBC(4)=[1 1 1 1 1; 0 1 1 1 1; 0 0 1 1 0 ; 0 0 0 1 0 ; 0 0 0 0 0]
DBIBC(5)=[1 1 1 1 1; 0 1 1 1 1; 0 0 0 0 0 ; 0 0 0 0 0 ; 0 0 0 0 1]

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

採用された回答

Daniel Abajo
Daniel Abajo 2020 年 6 月 2 日
Hi,
You need to re-inizialize the temporary matrix DBIBC for each iteration, if not the first iteration makes 2-5 rows 0 and thats all....
BIBC = [1 1 1 1 1;0 1 1 1 1;0 0 1 1 0;0 0 0 1 0;0 0 0 0 1] ;
N=length(BIBC);
for i=1:N
DBIBC = BIBC;
rw = find(DBIBC(:,i)==0)
DBIBC(rw,:)= 0
DBIBC(i)
B{i} = DBIBC;
end
  1 件のコメント
Luqmann Mahama
Luqmann Mahama 2020 年 6 月 2 日
Thank you so much. You’re a savior. IT WORKS!!!!

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

その他の回答 (1 件)

Daniel Abajo
Daniel Abajo 2020 年 6 月 2 日
Actually is shorter, faster and smart the following code, see that the isequal returns true...
BIBC = [1 1 1 1 1;0 1 1 1 1;0 0 1 1 0;0 0 0 1 0;0 0 0 0 1] ;
DBIBC=repmat(BIBC,1,1,size(BIBC,2));
DBIBC2=permute(repmat(BIBC,1,1,size(BIBC,2)),[1,3,2]);
DBIBC(find(DBIBC2==0))=0;
N=length(BIBC);
for i=1:N
DBIBC = BIBC;
rw = find(DBIBC(:,i)==0)
DBIBC(rw,:)= 0
DBIBC(i)
B{i} = DBIBC;
end
isequal(reshape(cell2mat(B),size(BIBC,1),size(BIBC,2),[]),DBIBC)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by